使用junit測試框架,測試查找用戶和添加用戶功能
com.tsh.test.xmlUserDaoTest
package com.tsh.test; import org.junit.Test; import com.tsh.dao.XmlUserDao; import com.tsh.domain.User; /** * 測試用例 * @author taoshihan * */ public class xmlUserDaoTest { @Test public void testFindUserByUsername(){ XmlUserDao dao=new XmlUserDao(); User user= dao.findUserByUsername("taoshihan"); System.out.println(user); } @Test public void testAddUser(){ XmlUserDao dao=new XmlUserDao(); User user=new User(); user.setUsername("taoshihan"); user.setPassword("123456"); dao.addUser(user); } }
在邏輯層service層中,拋出自定義異常
com.tsh.service.UserService
package com.tsh.service; import com.tsh.dao.XmlUserDao; import com.tsh.domain.User; import com.tsh.exception.MsgException; /** * 用戶邏輯 * @author taoshihan * */ public class UserService { /** * 用戶注冊 * @param user * @throws MsgException */ public void registerUser(User user) throws MsgException{ //檢查用戶名是否存在 XmlUserDao dao=new XmlUserDao(); if(dao.findUserByUsername(user.getUsername())!=null){ throw new MsgException("用戶名已經存在"); } dao.addUser(user); } }
在jsp中使用el標簽判斷登陸狀態
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用戶中心</title> </head> <body> <h1>用戶中心</h1> <hr> <c:if test="${sessionScope.user==null }"> 歡迎光臨,游客! <a href="${pageContext.request.contextPath }/login.jsp">登陸</a> <a href="${pageContext.request.contextPath }/register.jsp">注冊</a> </c:if> <c:if test="${sessionScope.user!=null }"> 歡迎光臨,${sessionScope.user.username }! <a href="${pageContext.request.contextPath }/Servlet/logout">注銷</a> </c:if> </body> </html>
在Servlet中對發送過來的數據進行處理
com.tsh.web.LoginServlet
package com.tsh.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.tsh.dao.XmlUserDao; import com.tsh.domain.User; /** * 登陸處理 */ public class LoginServlet extends HttpServlet { /** */ public LoginServlet() { super(); } /** */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("sss"); } /** */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); if("".equals(username) || "".equals(password==null)){ request.setAttribute("msg", "用戶名和密碼不能為空!"); request.getRequestDispatcher("/login.jsp").forward(request, response); return ; } XmlUserDao dao=new XmlUserDao(); User user=dao.findUserByUsername(username); if(user!=null && user.getPassword().equals(password)){ request.getSession().setAttribute("user", user); response.getWriter().write("登陸成功!"); }else{ response.getWriter().write("用戶名或密碼錯誤"); } } }