欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java用户管理注册功能 含前后台代码

程序员文章站 2024-03-13 08:47:33
本文实例为大家分享了java用户注册功能,供大家参考,具体内容如下 主页面: 浏览器显示: 代码实现: <%@ page langu...

本文实例为大家分享了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)注册成功:

java用户管理注册功能 含前后台代码

2)注册失败:

java用户管理注册功能 含前后台代码

代码实现:

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();
  }

}

查看所有用户功能:

浏览器显示:

java用户管理注册功能 含前后台代码

代码实现: 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两个包导入的相应的位置:

java用户管理注册功能 含前后台代码

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。