From the previous user management system we will find that JSP is mainly to do the interface, but the use of JSP to authenticate users and paging processing, and servlet processing page is the quickest and most convenient, so the user Management system framework has been improved, the use of MVC mode.
MVC is a design pattern that makes it mandatory to separate the input, processing, and output of an application. Using an MVC application is divided into three core parts: models, views, and controllers. Each of them handles their own tasks.
The individual letters of MVC are M (model models), V (view view), and C (Controller controllers)
M is mainly handled by Java class, or it can be Java BEAN,EJB, etc.
V Handled by JSP
c is handled by the servlet.
The simple framework diagram for the MVC pattern is as follows:
Through the analysis of the problem, we can improve the program:
Add the controller, replace the logincl.jsp with a servlet, and in the controller to call the model to complete the user's verification, play the servlet in the control of the advantages, can be improved as the following framework:
The implementation code is as follows:
View section:
1. Login interface (login.jsp)
<%@ page language= "java" contenttype= "text/html; charset=gb2312 " pageencoding=" gb2312 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
2. Welcome page (WEL.JSP)
<%@ page language= "java" contenttype= "text/html; charset=gb2312 "import=" java.sql.*,java.util.*,com.chongqing.model.* "pageencoding=" gb2312 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Model part:
1.conndb.java
Connect to database package Com.chongqing.model;import java.sql.*; public class Conndb {private Connection ct = null; Public Connection Getconn () { try { //Connect to Database Class.forName (" Com.microsoft.sqlserver.jdbc.SQLServerDriver "); ct = drivermanager.getconnection ("Jdbc:sqlserver://127.0.0.1:1433;databasename=testservlet", "sa", "123456"); } catch (Exception ex) { ex.printstacktrace (); } Do not close resource return ct;} }
2.userbean.java
Mapped to the users table in the database, one of its objects corresponds to a record in the Users table, and the operation UserBean is equivalent to manipulating the database package Com.chongqing.model;public class UserBean { private int userId; Private String userName; Private String passwd; Private String Mail; private int grade; public int getUserId () {return userId;} Public String GetUserName () {return userName;} Public String getpasswd () {return passwd;} Public String Getmail () {return mail;} public int Getgrade () {return grade;} public void Setuserid (int userId) {this.userid = userId;} public void Setusername (String userName) {this.username = UserName;} public void setpasswd (String passwd) {this.passwd = passwd;} public void Setmail (String mail) {this.mail = mail;} public void Setgrade (int grade) {This.grade = grade;} }
3.userbeancl.java
Business logic and processing classes, used to process the users table, that is, Operation Userbeanpackage Com.chongqing.model;import java.sql.*; Import java.util.*;p Ublic class Userbeancl {private Connection ct = null; Private PreparedStatement PS = null; Private ResultSet rs = null; private int pagecount = 0; Total number of pages (obtained by calculation) Private int pageSize = 3; 3 records per page//Verify user public boolean checkUser (String u,string p) {Boolean flag = false; try {//Get connected conndb cd = new Conndb (); ct = Cd.getconn (); PS = ct.preparestatement ("SELECT * from users where username =?") and passwd =? "); Ps.setstring (1,u); Ps.setstring (2,P); rs = Ps.executequery (); if (Rs.next ()) {flag = true; }} catch (Exception ex) {ex.printstacktrace (); } finally {this.close (); } return flag; }//page display results//cannot return resultset, because ResultSet will also disappear when database resources are closed, here is the return ArrayList public arraylist<object& Gt Getresultbypage (int pagenow) {arraylist<object> Al = new Arraylist<object> (); try {int rowCount = 0; How many lines of records (obtained by looking up the table)//Connection database Conndb cd = new Conndb (); ct = Cd.getconn (); PS = Ct.preparestatement ("SELECT count (*) from users"); rs = Ps.executequery (); if (Rs.next ()) {RowCount = Rs.getint (1);//Get RowCount Value}//Calculate PAGEC Ount value if (rowcount%pagesize = = 0) {PageCount = rowcount/pagesize; }else{PageCount = rowcount/pagesize + 1; } PS = Ct.preparestatement ("Select Top" +pagesIze+ "* from the users where UserID not in (select Top" +pagesize* (pageNow-1) + "UserID from users"); rs = Ps.executequery (); while (Rs.next ()) {//encapsulates each record in RS into UserBean UB UserBean UB = new Userbea N (); Ub.setuserid (Rs.getint (1)); Ub.setusername (rs.getstring (2)); UB.SETPASSWD (Rs.getstring (3)); Ub.setmail (Rs.getstring (4)); Ub.setgrade (Rs.getint (5)); Al.add (UB); Add UB to ArrayList}} catch (Exception ex) {Ex.printstackt Race (); } finally {this.close (); } return Al; }//returns PageCount public int getpagecount () {return this.pagecount; }//Close resource public void close () {try { if (null! = rs) {rs.close (); rs = null; } if (null! = PS) {ps.close (); PS = null; } if (null! = CT) {ct.close (); ct = null; }} catch (SQLException e) {e.printstacktrace (); } } }
Controller module:
1. Verify that the user
This is the controller, the main completion of the authentication of the user identity//controller itself is not going to complete the business logic, it is mainly through the call model to complete the processing of data package Com.chongqing.controller;import Java.io.ioexception;import Java.util.arraylist;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.chongqing.model.UserBeanCl, @WebServlet ("/loginclservlet") public class Loginclservlet extends HttpServlet { Private static final Long serialversionuid = 1l;protected void doget (HttpServletRequest request, HttpServletResponse resp Onse) throws Servletexception, IOException {//Receive user name and password, complete authentication to user string u = Request.getparameter ("username"); String p = request.getparameter ("passwd");//to the database for validation userbeancl UBC = new Userbeancl (); if (Ubc.checkuser (u,p)) {//Legal, go to the Welcome page//When you go to the wel.jsp page, put the data you want to show to wel.jsp ready arraylist<object> al = (Arrayl ist<object>) Ubc.getresultbypage (1); int pagecount = Ubc.getpagecount (); Request.setattribute ("Result", AL); Request.setattribute ("PageCount", pagecount+ ""); Request.setattribute ("Pagenow", "1"); Response.sendredirect ("wel.jsp?user=" +u); Because the Sendredirect method is inefficient, software companies often use a forwarding method//This method is efficient, while the request object can also use Request.getrequestdispatcher ("wel.jsp") on the next page . Forward (request, response); }else{//Illegal, go to the login page request.getrequestdispatcher ("login.jsp"). Forward (request, response); }}protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {This.doget (request, Response);}}
2. Paging function
This is the controller that will be paged out package Com.chongqing.controller;import Java.io.ioexception;import Java.util.arraylist;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.chongqing.model.UserBeanCl, @WebServlet ("/userclservlet") public class Userclservlet extends HttpServlet {private Static final Long serialversionuid = 1l;protected void doget (HttpServletRequest request, httpservletresponse response) th Rows Servletexception, ioexception {int pagenow = Integer.parseint (Request.getparameter ("S_pagenow")); Userbeancl UBC = new Userbeancl (); Arraylist<object> al = (arraylist<object>) ubc.getresultbypage (Pagenow); int pagecount = Ubc.getpagecount (); Request.setattribute ("Result", AL); Request.setattribute ("PageCount", pagecount+ ""); Request.setattribute ("Pagenow", pagenow+ ""); Request.getrequestdispatchER ("wel.jsp"). Forward (request, response);} protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { This.doget (request, Response);}}
MVC model of User Management System website Framework Improvement