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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)