[JavaEE] Three-tier architecture case study-User Module (2), javaee three-tier
Use the junit testing framework to test the function of searching for and adding users
Com. tsh. test. xmlUserDaoTest
Package com. tsh. test; import org. junit. test; import com. tsh. dao. xmlUserDao; import com. tsh. domain. user;/*** Test case * @ 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 User = new user (); user. setUsername ("taoshihan"); user. setPassword ("123456"); dao. addUser (user );}}
In the service layer of the logic layer, a custom exception is thrown.
Com. tsh. service. UserService
Package com. tsh. service; import com. tsh. dao. xmlUserDao; import com. tsh. domain. user; import com. tsh. exception. msgException; /*** user logic ** @ author taoshihan **/public class UserService {/*** User registration ** @ param user * @ throws MsgException */public void registerUser (user User user) throws MsgException {// check whether the user name has XmlUserDao dao = new XmlUserDao (); if (dao. findUserByUsername (user. getUsername ())! = Null) {throw new MsgException ("the user name already exists");} dao. addUser (user );}}
Use the el tag in jsp to determine the login status
<% @ 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">
Process the sent data in 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;/*** login Processing */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", "the user name and password cannot be blank! "); 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 ("Login successful! ") ;}Else {response. getWriter (). write (" Incorrect username or password ");}}}