黑马day07 登录注冊案例
Posted gavanwanggw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了黑马day07 登录注冊案例相关的知识,希望对你有一定的参考价值。
简单介绍:依据三层架构的思想设计本案例。
1.搭建好开发环境
准备好须要的包和模拟数据库配置文件users.xml
-->cn.itheima.dao
-->cn.itheima.service
-->cn.itheima.domain
-->cn.itheima.web
-->cn.itheima.utils
-->cn.itheima.exception
-->cn.itheima.test
2.建立模拟数据库users.xml
<?xml version="1.0" encoding="UTF-8"?> <users> <user username="admin" password="admin" nickname="admin" email="[email protected]"></user> </users>3.建立javaBean users
package cn.itheima.domain; import cn.itheima.exception.MsgException; public class User { private String username; private String password; private String password2; private String nickname; private String email; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPassword2() { return password2; } public void setPassword2(String password2) { this.password2 = password2; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public void check()throws MsgException{ if(username==null || "".equals(username)){ throw new MsgException("用户名不能为空!"); } if(password==null || "".equals(password)){ throw new MsgException("密码不能为空!"); } if(password2==null || "".equals(password2)){ throw new MsgException("确认密码不能为空!"); } if(!password.equals(password2)){ throw new MsgException("两次密码不一致!"); } if(nickname==null || "".equals(nickname)){ throw new MsgException("昵称不能为空!"); } if(email==null || "".equals(email)){ throw new MsgException("邮箱不能为空!"); } if(!email.matches("^\\[email protected]\\w+(\\.\\w+)+$")){ throw new MsgException("邮箱格式不对!"); } } }
4.建立UserDao...实现对应的查询和加入功能(主要通过使用dom树操作xml文件)
package cn.itheima.dao; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import cn.itheima.domain.User; import cn.itheima.utils.XmlUtil; public class XmlUserDao { /*** * 依据用户名查询用户 * @param username用户名 * @return 依据用户名找到用户的信息封装到javaBean中,没有返回null */ public User findUserByUsername(String username){ Document doc = XmlUtil.getDocument(); Element root = doc.getRootElement(); List<Element> list = root.selectNodes("//user[@username='"+username+"']"); if(list.size()>0){//假设找到这个用户就封装到bean中返回user Element userEle = list.get(0); User user=new User(); user.setUsername(userEle.attributeValue("username")); user.setPassword(userEle.attributeValue("password")); user.setNickname(userEle.attributeValue("nickname")); user.setEmail(userEle.attributeValue("email")); return user; }else{//没有找到用户 return null; } } /** * 通过username与password查找用户,找到就封装到javaBean中,找不到返回null * @param username * @param password * @return */ public User findUserByUserNameAndPassword(String username,String password){ Document doc = XmlUtil.getDocument(); Element root = doc.getRootElement(); List<Element> list = root.selectNodes("//user[@username='"+username+"' and @password='"+password+"']"); if(list.size()>0){//假设找到这个用户就封装到bean中返回user Element userEle = list.get(0); User user=new User(); user.setUsername(userEle.attributeValue("username")); user.setPassword(userEle.attributeValue("password")); user.setNickname(userEle.attributeValue("nickname")); user.setEmail(userEle.attributeValue("email")); return user; }else{//没有找到用户 return null; } } /** * 加入一个用户到数据库中 * @param user */ public void addUserToXml(User user){ Document doc = XmlUtil.getDocument(); Element root = doc.getRootElement(); //1.凭空创建一个<user>标签,并设置属性 Element userEle = DocumentHelper.createElement("user"); userEle.setAttributeValue("username", user.getUsername()); userEle.setAttributeValue("password", user.getPassword()); userEle.setAttributeValue("nickname", user.getNickname()); userEle.setAttributeValue("email", user.getEmail()); //2.挂在到root标签下 root.add(userEle); //3.改动xml文件 XmlUtil.updateXml(); } }5.建立一个工具类提供获取document和改动xml的方法
package cn.itheima.utils; import java.io.FileOutputStream; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XmlUtil { private static Document doc=null; private static String path=XmlUtil.class.getClassLoader().getResource("users.xml").getPath(); static{ SAXReader reader=new SAXReader(); try { doc = reader.read(path); } catch (DocumentException e) { e.printStackTrace(); } } /** * 返回文档对象 * @return */ public static Document getDocument(){ return doc; } /** * 改动xml文件 */ public static void updateXml(){ try { XMLWriter writer=new XMLWriter(new FileOutputStream(path),OutputFormat.createPrettyPrint()); writer.write(doc); writer.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(); } } }6.开发service 提供方法注冊的方法和检查username和password的方法
package cn.itheima.service; import cn.itheima.dao.XmlUserDao; import cn.itheima.domain.User; import cn.itheima.exception.MsgException; public class UserSevice { XmlUserDao dao=new XmlUserDao(); /** * 注冊用户 * @param user * @throws MsgException */ public void registUser(User user) throws MsgException{ //1.推断数据库中是不是实用户了 if(dao.findUserByUsername(user.getUsername())!=null){ throw new MsgException("用户已经存在"); } //2.加入到数据库中 dao.addUserToXml(user); } /** * 检查username与password是否正确 * @param username * @param password * @return */ public User isUser(String username,String password){ return dao.findUserByUserNameAndPassword(username, password); } }7.建立异常类MsgExcetion
package cn.itheima.exception; public class MsgException extends Exception { public MsgException() { } public MsgException(String msg) { super(msg); } }
8.开发案例的主页面index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <c:if test="${sessionScope.user==null }"> 欢迎光临 游客!<hr/><a href="${pageContext.request.contextPath}/regist.jsp">注冊</a><hr><a href="${pageContext.request.contextPath}/login.jsp">登录</a> </c:if> <c:if test="${sessionScope.user!=null}"> 欢迎光临${sessionScope.user.username }!执行结果示意图:<hr><a href="${pageContext.request.contextPath}/login.jsp">登录</a><hr/><a href="${pageContext.request.contextPath}/servlet/LogoutServlet">注销</a> </c:if> </body> </html>
以上是关于黑马day07 登录注冊案例的主要内容,如果未能解决你的问题,请参考以下文章
JDK安装以及HelloWorld案例命令行编译运行-Java入门-黑马程序员2022年新视频DAY1笔记
黑马旅游网:用户登录,导航条,线路分页——Java就业班day47