程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java用戶治理注冊功效 含前後台代碼

java用戶治理注冊功效 含前後台代碼

編輯:關於JAVA

java用戶治理注冊功效 含前後台代碼。本站提示廣大學習愛好者:(java用戶治理注冊功效 含前後台代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java用戶治理注冊功效 含前後台代碼正文


本文實例為年夜家分享了java用戶注冊功效,供年夜家參考,詳細內容以下

主頁面:

閱讀器顯示:

代碼完成:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
 <head>

  <title>用戶信息治理</title>
 </head>

 <body>
  用戶注冊:<br/>
  <!-- 絕對途徑 -->
  <form action="reg" method="post">
    姓名:<input type="text" name="name"/><br/>
    暗碼:<input type="password" name="pwd"/><br/>
    <input type="submit" value="注冊"/>
  </form>
  <!-- 相對途徑 -->
  <a href="/usersWeb/reg">檢查一切用戶</a><!-- GET方法:超鏈接和在url前面添加參數 -->
 </body>
</html>

注冊功效:

閱讀器顯示:

1)注冊勝利:


2)注冊掉敗:

代碼完成:

package cn.hncu.user.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hncu.user.dao.UserDao;
import cn.hncu.user.dao.UserDaoImpl;
import cn.hncu.user.domain.User;

public class RegServlet extends HttpServlet {

  //注入
  UserDao dao=new UserDaoImpl();
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    List<User> users=dao.queryAllUsers();


    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println(" <HEAD><TITLE>注冊成果頁面</TITLE></HEAD>");
    out.println(" <BODY>");

    out.println("<table cellspacing=0 border=1px><tr><th>操作</th><th>姓名</th></tr>");
    for(User u:users){
      out.println("<tr><td><a href='/usersWeb/del?id="+u.getId()+"'>刪除</a></th>");
      out.println("<th>"+u.getName()+"</th></tr>");
    }
    out.println("</table>");


    out.println(" </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
  }


  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    request.setCharacterEncoding("utf-8");
    String name=request.getParameter("name");
    String pwd=request.getParameter("pwd");
    User user=new User();
    user.setName(name);
    user.setPwd(pwd);
    boolean boo=dao.reg(user);



    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println(" <HEAD><TITLE>注冊成果頁面</TITLE></HEAD>");
    out.println(" <BODY>");


    if(boo){
      out.println("祝賀您("+name+")注冊勝利了...");
    }else{
      out.println("您("+name+")注冊掉敗了...<br/>用戶名已存在,請換一個注冊");
    }
    out.println("<a href='javascript:history.go(-1)'>前往</a>");
    out.println(" </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
  }

}

檢查一切用戶功效:

閱讀器顯示:

代碼完成: RegServlet類的doGet辦法完成

刪除操作功效:刪除勝利會重定向到主頁面

代碼完成:

package cn.hncu.user.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hncu.user.dao.UserDao;
import cn.hncu.user.dao.UserDaoImpl;

public class DelServlet extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String id=request.getParameter("id");
//   System.out.println("del-id:"+id);
    UserDao dao=new UserDaoImpl();
    dao.delUser(id);

    //轉到主頁--重定向
    response.sendRedirect("/usersWeb/index.jsp");
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    doGet(request,response);
  }

}

設置裝備擺設文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <display-name></display-name>
 <servlet>
  <servlet-name>RegServlet</servlet-name>
  <servlet-class>cn.hncu.user.servlet.RegServlet</servlet-class>
 </servlet>
 <servlet>
  <servlet-name>DelServlet</servlet-name>
  <servlet-class>cn.hncu.user.servlet.DelServlet</servlet-class>
 </servlet>


 <servlet-mapping>
  <servlet-name>RegServlet</servlet-name>
  <url-pattern>/reg</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>DelServlet</servlet-name>
  <url-pattern>/del</url-pattern>
 </servlet-mapping>  
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

domain包的User對象

package cn.hncu.user.domain;

public class User {

  private String id;
  private String name;
  private String pwd;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPwd() {
    return pwd;
  }
  public void setPwd(String pwd) {
    this.pwd = pwd;
  }
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    User other = (User) obj;
    if (id == null) {
      if (other.id != null)
        return false;
    } else if (!id.equals(other.id))
      return false;
    return true;
  }
  @Override
  public String toString() {
    return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
  }

}

邏輯層dao:

接口



完成類

package cn.hncu.user.dao;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import cn.hncu.user.domain.User;
import cn.hncu.user.utils.DomFactory;
import cn.hncu.user.utils.IDGenerate;

public class UserDaoImpl implements UserDao{

  Document dom=DomFactory.getDom();
  @Override
  public boolean reg(User user) {
    //  //title[@lang='eng']拔取一切 title 元素,且這些元素具有值為 eng 的 lang 屬性。
//   dom.selectSingleNode("//user[fn:lower-case(@name)='"+user.getName().toLowerCase()+"']");
    Node node=dom.selectSingleNode("//user[name='"+user.getName()+"']");//user節點下值為user.getName()的name元素
    //"name[='"+user.getName().toLowerCase()+"']"
    if(node!=null){
      return false;
    }

    Element root=dom.getRootElement();
    Element eUser=root.addElement("user");
    String id=IDGenerate.getID();
    eUser.addAttribute("id", id);
    eUser.addElement("name").setText(user.getName());
    eUser.addElement("pwd").setText(user.getPwd());

    DomFactory.save();


    return true;
  }

  @Override
  public List<User> queryAllUsers() {
    List<User> users=new ArrayList<User>();
    List<Element> en=dom.selectNodes("//user");
    for(Element e:en){
      String id=e.attributeValue("id");
      String name=e.elementText("name");
      User user=new User();
      user.setId(id);
      user.setName(name);
      users.add(user);
    }

    return users;
  }

  @Override
  public void delUser(String id) {
//   System.out.println(id);
    String xpath="//user[@id='"+id+"']";
    Node node=dom.selectSingleNode(xpath);
    if(node!=null){
//     System.out.println("del...");
      node.getParent().remove(node);
      DomFactory.save();
    }

  }

}

對象包utils:
獲得用戶id

package cn.hncu.user.utils;

import java.util.UUID;

public class IDGenerate {
  private IDGenerate(){

  }
  public static String getID(){
//   return UUID.randomUUID().toString();
    return UUID.randomUUID().toString().replace("-", "");
  }
}

對數據庫users.xml的dom獲得和保留辦法

這裡用dom4j的包,,,所以要把dom4j兩個包導入的響應的地位:

package cn.hncu.user.utils;

import java.io.FileOutputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class DomFactory {
  private static Document dom;
  private static String fileName;
  static{
    try {
      SAXReader r=new SAXReader();
      fileName=DomFactory.class.getClassLoader().getResource("users.xml").getPath();
      System.out.println("users.xml的途徑:"+fileName);///D:/apache-tomcat-7.0.30/webapps/usersWeb/WEB-INF/classes/users.xml
      //留意:獲得tomcat中以後項目classpath下的資本方法
      dom=r.read(fileName);
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }
  public static Document getDom(){
    return dom;
  }
  public static void save(){
    XMLWriter w;
    try {
      w = new XMLWriter(new FileOutputStream(fileName));
      w.write(dom);
      w.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

數據庫:

<?xml version="1.0" encoding="UTF-8"?>
<users>

</users>

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved