[Java EE] Three-tier architecture case-user module (ii)

Source: Internet
Author: User

Test Find users and add user features using the JUnit test framework

Com.tsh.test.xmlUserDaoTest

 Packagecom.tsh.test;Importorg.junit.Test;ImportCom.tsh.dao.XmlUserDao;ImportCom.tsh.domain.User;/*** Test Case *@authorTaoshihan **/ Public classxmluserdaotest {@Test Public voidTestfinduserbyusername () {Xmluserdao DAO=NewXmluserdao (); User User= Dao.finduserbyusername ("Taoshihan");    SYSTEM.OUT.PRINTLN (user); } @Test Public voidTestadduser () {Xmluserdao DAO=NewXmluserdao (); User User=NewUser (); User.setusername ("Taoshihan"); User.setpassword ("123456");    Dao.adduser (user); }}

In the logic Layer service layer, throw a custom exception

Com.tsh.service.UserService

 PackageCom.tsh.service;ImportCom.tsh.dao.XmlUserDao;ImportCom.tsh.domain.User;Importcom.tsh.exception.MsgException;/*** User Logic *@authorTaoshihan **/ Public classUserService {/*** User Registration *@paramUser *@throwsmsgexception*/     Public voidRegisterUser (user user)throwsmsgexception{//Check if the user name existsXmluserdao dao=NewXmluserdao (); if(Dao.finduserbyusername (User.getusername ())! =NULL){            Throw NewMsgexception ("User name already exists");    } dao.adduser (user); }}

use el tags in jsp to determine 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" >ifTest= "${sessionscope.user==null}" >Welcome, visitors! <a href= "${pagecontext.request.contextpath}/login.jsp" > Login </a> <a href= "${ PageContext.request.contextPath}/register.jsp "> Registered </a></c:if><c:ifTest= "${sessionscope.user!=null}" >Welcome, ${sessionscope.user.username}! <a href= "${pagecontext.request.contextpath}/servlet/logout" > Logout </a></c:if></body>

processing the data sent over the Servlet

Com.tsh.web.LoginServlet

 PackageCom.tsh.web;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.tsh.dao.XmlUserDao;ImportCom.tsh.domain.User;/*** Login Processing*/ Public classLoginservletextendsHttpServlet {/**     */     PublicLoginservlet () {Super(); }    /**     */    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.getwriter (). Write ("SSS"); }    /**     */    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, 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", "User name and password cannot be empty!" "); Request.getrequestdispatcher ("/login.jsp"). Forward (request, response); return ; } Xmluserdao DAO=NewXmluserdao (); User User=Dao.finduserbyusername (username); if(user!=NULL&&User.getpassword (). Equals (password)) {request.getsession (). SetAttribute ("User", user); Response.getwriter (). Write ("Successful Landing!" "); }Else{response.getwriter (). Write ("User name or password error"); }    }}

[Java EE] Three-tier architecture case-user module (ii)

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.