Read the Hanshunping of the Java EE video, bar inside taught to do out, feel great, novice road!!!
This certainly is not original, but is not reproduced, then I would like to use it as a translator, to other video learning but no code of the students a little welfare bar
The first is the login.jsp login interface code.
<%@ page language= "java" import= "java.util.*" pageencoding= "gb2312"%><%string path = Request.getcontextpath ( ); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Then there is the code for the Welcome interface, wel.jsp<%@ page language= "java" import= "java.util.*,java.sql.*,com.xf.model.*" pageencoding= "gb2312"%><%string Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Then is the servlet file controller, Loginclservlet.java this is processing the account password from login.jsp/** * Function: This is a controller, to complete the authentication of the user identity, itself is not going to complete the business logic * It is mainly to call model complete processing of data * Time: June 5, 2014 12:56:46 * cutter_point */package Com.xf.controller;import com.xf.model.*;import java.io.ioexception;import Java.io.printwriter;import java.util.*; Import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class LoginClServlet Extends httpservlet{/** * The Doget method of the servlet. <br> * This method was called when a form have its tag VA Lue method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void Doget (Http ServletRequest request, HttpServletResponse response) throws Servletexception, ioexception{//get account password string u= Request.getparameter ("username");//Account String P=request.getparameter ("passwd");//SecretThe code//call model validates him USERBEANCL ubc=new userbeancl (), if (Ubc.checkuser (U, p)) {System.out.println ("This is the servlet that is used to complete the validation!! ");//Legal//Jump//response.sendredirect (" wel.jsp ")/////To get the data ready before entering the wel.jsp//first display is the default first page ArrayList al= Ubc.getusersbypage (1); int pagecount=ubc.getpagecount (); Request.setattribute ("Result", AL); Request.setattribute (" PageCount ", pagecount+" "), Request.setattribute (" Pagenow "," 1 ");//But the above method is inefficient, and the efficiency is Request.getrequestdispatcher (" Wel.jsp?user= "+u). Forward (request, response);} else{//illegal//response.sendredirect ("login.jsp");//Reason ditto Request.getrequestdispatcher ("login.jsp"). Forward (Request , response);}} /** * The DoPost method of the servlet. <br> * This method was called when a form have its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void DoPost (HTT Pservletrequest request, HttpserVletresponse response) throws Servletexception, Ioexception{this.doget (request, Response);}}
Then there is another controller, that is, the landing into the section of the page jump Userclservlet.java/** * Function: This controller will handle the user's paging display, the user's delete modify query, add * Time: June 5, 2014 13:47:03 * cutter_point */package Com.xf.controller;import Com.xf.model.*;import Java.io.ioexception;import Java.io.printwriter;import Java.util.arraylist;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class UserClServlet Extends httpservlet{/** * The Doget method of the servlet. <br> * This method was called when a form have its tag VA Lue method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void Doget (Http ServletRequest request, HttpServletResponse response) throws Servletexception, ioexception{//get the pagenowstring s that the user wants to display _pagenow=request.getparameter ("Pagenow");//Use Userbeancl method call to get Pagenowtry{int Pagenow=integeR.parseint (S_pagenow);//Call Userbeancluserbeancl ubc=new Userbeancl ();//Prepare the data before entering wel.jsp ArrayList al= Ubc.getusersbypage (pagenow); int pagecount=ubc.getpagecount (); Request.setattribute ("Result", AL); Request.setattribute ("PageCount", pagecount+ ""); Request.setattribute ("Pagenow", pagenow+ "");// Request.setattribute ("Pagenow", Pagenow); No, it has to be quoted in the back, otherwise it won't work, because others//default bar Pagenow as a string, the result passed, thought is a string type, is actually an int type, the result is wrong System.out.println (" The paging is using the userclservlet!! "); /Jump back to wel.jsp inside to Request.getrequestdispatcher ("wel.jsp"). Forward (request, response);} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}} /** * The DoPost method of the servlet. <br> * This method was called when a form have its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void DoPost (HTT Pservletrequest REQuest, HttpServletResponse response) throws Servletexception, Ioexception{this.doget (request, Response);}}
Then there are three model files connected to the database, is used to be the above several calls!!respectively: Userbean.java,userbeancl.java,conndb.java
/** * Function: This is a javabean, corresponding to the users table, representing the data * Time: June 4, 2014 10:39:33 * cutter_point */package Com.xf.model;public class userbean{private int userid;private String username;private string passwd;private string email;private int grade;public i NT GetUserId () {return userId;} public void Setuserid (int userId) {this.userid = userId;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String getpasswd () {return passwd;} public void setpasswd (String passwd) {this.passwd = passwd;} Public String Getemail () {return email;} public void Setemail (String email) {this.email = email;} public int Getgrade () {return grade;} public void Setgrade (int grade) {This.grade = grade;}}
/** * Function: This is a processing class, also called Bo, the main package for the users table of various operations * each Userbean object into the ArrayList inside, earlier close and database link * Time: June 4, 2014 10:43:48 * cutter_point */package com.xf.model;import java.sql.*;import java.util.*;p ublic class userbeancl{private Statement sm=null;private ResultSet rs=null;private Connection ct=null;private int pagesize=3;//default starting from first page private int rowcount=0;// This query from the database private int pagecount=0;//through ROWCOUNT and pagesize to obtain public void close () {try{if (rs! = null) {rs.close (); rs=null;} if (SM! = null) {sm.close (); sm=null;} if (ct! = null) {ct.close (); ct=null;}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Get PageCount, page total public int getpagecount () {try{//get link ct=new conndb (). Getconn ();//declaration, creating a Smsm=ct.createstatement ( );//4. Query the database for resultset rs=sm.executequery ("SELECT count (*) from users");//Note here must next, otherwise GG dense if (Rs.next ()) { Rowcount=rs.getint (1);} Calculates the result of the PageCount if (rowcount%pagesize = = 0) {pagecount=rowcount/pagesize;} else{pagecount=rowcount/pagesize+1;}} catch (Exception e) {//Todo:handle ExceptionE.printstacktrace ();} Finally{this.close ();} return PageCount;} Get user needs to display the information (paging) public ArrayList getusersbypage (int pagenow) {ArrayList a1=new ArrayList (); try{//get link ct=new conndb () . Getconn ();//create statementsm=ct.createstatement ();//query out the need to display the information rs=sm.executequery ("Select Top" +pagesize+ "* from The users where UserId is not in (select Top "+pagesize* (pageNow-1) +" userId from Users) "), and/or the information obtained is stored on the ArrayList (Rs.next ()) {UserBean ub=new UserBean (); Ub.setuserid (Rs.getint (1)); Ub.setusername (Rs.getstring (2)); UB.SETPASSWD ( Rs.getstring (3)); Ub.setemail (Rs.getstring (4)); Ub.setgrade (Rs.getint (5));//Put UB in ArrayList to A1.add (UB);}} catch (Exception e) {e.printstacktrace ();//Todo:handle exception}finally{//Close Resource this.close ();} return A1;} Check that the account password is correct public boolean checkUser (String u, String p) {Boolean b=false;try{//to the database to verify Ct=new conndb (). Getconn ();//3, Create a statementsm=ct.createstatement ();//4, query, put the query first result in RS rs=sm.executequery ("Select passwd from Users where Username= ' "+u+");//depending on the result to determine whether the data to be searched if (Rs.next ()) {//User name exists, verify the password if (rs.getstring (1). Equals (p)) {b=true;}}} catch (Exception e) {e.printstacktrace ();//Todo:handle Exception}finally{this.close ();} return b;}}
/** * Function: Get the database link * Time: June 4, 2014 10:47:57 * cutter_point */package com.xf.model;import java.sql.*;p ublic class Conndb{priv Ate Connection ct=null;public Connection getconn () {TRY{//1, load drive Class.forName (" Com.microsoft.sqlserver.jdbc.SQLServerDriver ");//2, Get link ct=drivermanager.getconnection (" jdbc:sqlserver:// LOCALHOST:1433;DATABASENAME=SPDB1 "," sa "," ");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return CT;}}
PS: Continuous efforts, silently learn!!!