0. New Web Project
First, create a new Java Web project in MyEclipse, and the project name is login. At this point, the project contains a src folder and a Webroot folder, as well as a Java web-included JRE Library folder and a EE library folder. Where, under the Webroot folder, the Web-inf folder and a index.jsp paging file are included.
Next, create a new JSP page named login.jsp.
The project document is structured as follows:
1. Design login.jsp page
Open the Login.jsp page, modify the first line of code for pageencoding= "Utf-8" to prevent the page in Chinese garbled. Next, define form forms in the body section for users to enter user names and passwords. The page code is as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
2. New Servlet file
Next, create a new Loginservlet.java in MyEclipse and define it under Package folder A (the package name is your own decision). The folder directory is as follows:
Double-click to open the Loginservlet.java file and, within the Dopost method, get the username and password of the login page through the Request.getparameter () method, and jumps to the index.jsp page through the Response.sendredirect () method. The page code is as follows:
Package A;
Import java.io.IOException;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class Loginservlet implements javax.servlet.servlet{public void DoPost (HttpServletRequest request, HttpServletResponse response) throws servletexception,ioexception{String userName = Request.getparameter ("UserName")
//Get user name String password = request.getparameter ("password");//Get Password Response.sendredirect ("index.jsp");
public void Destroy () {} public ServletConfig Getservletconfig () {return null;
Public String Getservletinfo () {return null; } public void init (ServletConfig arg0) throws servletexception {} public void service (ServletRequest request, serv Letresponse response) throws Servletexception, IOException {httpservletrequest RQ = (httpservletrequest) Request;
HttpServletResponse rs = (httpservletresponse) response;
DoPost (RQ,RS);
}
}
3. Configure Servlet
Open the Web-inf folder under the Web.xml file, by setting the homepage of this site as login.jsp. Next, configure the servlet. The page code is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"2.5" xmlns= "http://java.sun.com/xml/ns/"
Java ee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<servlet>
< Servlet-name>loginservlet</servlet-name>
<servlet-class>a.loginservlet</servlet-class >
</servlet>
<servlet-mapping>
<servlet-name>loginservlet</servlet-name >
<url-pattern>/login</url-pattern>
</servlet-mapping>
< welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. Test page
finally look at the run effect, select Run, select a version of Tomcat boot. The page effect is as follows:
5. Configure the data source
(1) Create a database
Open the MySQL database, create a new database, name the login database here, make the table login in the database, and set the username and password fields. The design table for the database is as follows:
The ID in the figure above is used as the primary key, making the table of the database conform to the requirements of the second normal form, where the username and password fields are unique fields in the login table, so this table conforms to the third normal form.
Next, fill in the login table with data, and the data for this example are as follows:
(2) Connecting to the database
connect the database in MyEclipse, where the JDBC-driven method is used to connect to the database. First download the MySQL driver, next need to import the MySQL jar package into our project, in Package Explorer right key JRE system library, select construction path-Configuration Build Path tab, pop-up Java Build Path tab, by adding external jar (X) button to load the Mysql.jar file.
Next, create a new Dbutil.java file in your project to connect to the MySQL database. The principle of connecting the database is omitted here, the source code of the Dbutil.java file is as follows, please change the database name in the source and the username password of the MySQL, etc. information:
Package A;
Import java.sql.*;
public class Dbutil {Boolean binited = false; Load driver public void Initjdbc () throws ClassNotFoundException {//load MySQL JDBC driver class.forname ("Com.mysql.jdbc.Driver")
;
Binited = true;
System.out.println ("Success loading Mysql driver!");
Public Connection getconnection () throws ClassNotFoundException, sqlexception{if (!binited) {initjdbc (); The///connection URL is jdbc:mysql//server address/Database name//The following 2 parameters are login username and password Connection conn = drivermanager.getconnection ("Jdbc:mysql://lo
calhost:3306/database name "," username "," password ");
Return conn;
public boolean loginsuccess (String username,string password) {Boolean returnvalue = false;
String sql = "SELECT * from Login";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{conn = getconnection ();
stmt = Conn.createstatement ();
rs = stmt.executequery (SQL);
while (Rs.next ()) {String usernameindb = rs.getstring ("username");
String passwordindb = rs.getstring ("password"); if (usernameindb.equals(userName) && passwordindb.equals (password)) {returnvalue = true;
Break
}}catch (ClassNotFoundException e) {e.printstacktrace ();
}catch (SQLException e) {e.printstacktrace ();
Return returnvalue;
}
}
In the Loginsuccess () method above, it is used to look up the user name and password in the database and match the incoming parameter username, password. Returns a true result once it is found.
(3) Modify servlet business logic
Modify the business logic of the Loginservlet.java file, in which you join the statement that connects the database. Where the main page jump logic is written in the Dopost () method, the modified Dopost () method is as follows:
public void DoPost (httpservletrequest request,httpservletresponse response)
throws Servletexception,ioexception {
String userName = Request.getparameter ("UserName");//Get user
name string password = Request.getparameter (" Password ")//Get password
dbutil db = new Dbutil ();//Build Database Object
Boolean canlogin = db.loginsuccess (userName, password);
if (canlogin) {//According to login, jump page
response.sendredirect ("index.jsp");
} else{
response.sendredirect ("login.jsp");
}
(4) test page
the page effect after debugging is as follows: