The previous blog on the Model1 and model2 a comparative summary, this blog on the idea of Model2 "instantiation", introduce a model2 landing example.
The example is simple: the user enters the user name and password on the index.jsp page, submits to the method that Servelt,servlet calls the manager, the manager makes a logical judgment, after judging the servlet will take the page to the show.jsp page, show.jsp will feedback the user login situation.
index.jsp
A text box with a user name and password on this page has been submitted to the Submit button on the page. When you click the Submit button the entire form will be submitted directly.
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
show.jsp
Used to feedback the user login information, the use of Jstl, the direct display of information.
<%@ page language= "java" contenttype= "text/html; charset=gb18030 " pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
servletGet the form data, invoke the business logic in the background, and make the page jump.
public class Loginservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {//Get the user name and user password sent by the foreground string Username=request.getparameter ("UserName"); String password=request.getparameter ("PassWord");//constructs the user object and assigns the user user=new user (); user.setusername (UserName); User.setpassword (PassWord);//query whether the user exists Usermanager usermanager=new usermanager (); String showinfo=usermanager.queryuser (user);//displays information into Rqeust request.setattribute ("Showinfo", showinfo);// Page redirected to Show.jsprequest.getRequestDispatcher ("/show.jsp"). Forward (request, response);}}
Manager
The manager stores the data as well as the business logic judgment, where it writes a bogus data and determines the business logic with the login username and password.
public class Usermanager {public String queryuser (user user) {User userdb=new User (); Userdb.setusername ("admin"); Userdb.setpassword ("admin"), if (User.getusername (). Equals (Userdb.getusername ()) && User.getpassword (). Equals (Userdb.getpassword ())) {return "Welcome" "+userdb.getusername () +" "Login System";} Return "user does not exist";}}
Time series diagram of a login instance
It is not difficult to find the time series diagram of the Model2 in the previous blog, and the sequence diagram of the login instance can correspond to the lifeline one by one of the Model2 timing diagram. The idea of a single layer of responsibility is also shown, JSP is used to display, the servlet is used to turn the page, and manger is equivalent to model for business logic.
Java Model2 User Login Simple instance