[JavaEE] Three-tier architecture case study-User Module (2), javaee three-tier

Source: Internet
Author: User

[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 ");}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.