JSP + Servlet + JavaBean to implement detailed login page instance, servletjavabean
This document describes how to use JSP + Servlet + JavaBean to log on to the web page. Share it with you for your reference. The details are as follows:
Four files are involved:
1. logon page: login.html
2. login successful welcome page: login_success.jsp
3. logon Failure page: login_failure.jsp
4. Servlet processing file: LoginServlet. java
In fact, there is also a file: web. xml, which will be discussed later:
These files are described as follows:
1. logon page: login.html
<! -- This Login page is a simple logon interface --> <! -- The JSP program is used to test the connection with the MySQL database. A database: LearnJSP is required, and a table: userinfo has two fields: UserName varchar (20) not null, userPwd varchar (20) not null -->
2. login successful welcome page: login_success.jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
3. logon Failure page: login_failure.jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
4. Servlet processing file: LoginServlet. java
/*** The JSP program is used to test the connection with the MySQL database. * A Database: LearnJSP and one of the tables: userinfo * have two fields: userName varchar (20) not null, UserPwd varchar (20) not null */package zieckey. login. servlet; import java. SQL. statement; import java. io. IOException; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import javax. servlet. servlet; import javax. servlet. servletException; import javax. servlet. htt P. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class LoginServlet extends HttpServlet implements Servlet {public LoginServlet () {// TODO Auto-generated constructor stub}/** (non-Javadoc) ** @ see javax. servlet. http. httpServlet # doGet (javax. servlet. http. httpServletRequest, * javax. servlet. http. httpServletResponse) * // @ Override pro Tected void doGet (HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {}/** (non-Javadoc) ** @ see javax. servlet. http. httpServlet # doPost (javax. servlet. http. httpServletRequest, * javax. servlet. http. httpServletResponse) * // @ Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {respo AUC. setContentType ("text/html"); String result = ""; // obtain the username String sUserName = request. getParameter ("txtUserName"); if (sUserName = "" | sUserName = null | sUserName. length ()> 20) {try {result = "Enter the user name (no more than 20 characters )! "; Request. setAttribute ("ErrorUserName", result); response. sendRedirect ("login.html");} catch (Exception e) {}}// obtain the password String sPasswd = request. getParameter ("txtPassword"); if (sPasswd = "" | sPasswd = null | sPasswd. length ()> 20) {try {result = "enter the password (no more than 20 characters )! "; Request. setAttribute ("ErrorPassword", result); response. sendRedirect ("login.html");} catch (Exception e) {}}// register the JDBC driver try {Class. forName ("org. gjt. mm. mysql. driver "). newInstance ();} catch (InstantiationException e) {// TODO Auto-generated catch block e. printStackTrace (); System. out. println ("InstantiationException");} catch (IllegalAccessException e) {// TODO Auto-g Enerated catch block e. printStackTrace (); System. out. println ("IllegalAccessException");} catch (ClassNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace (); System. out. println ("ClassNotFoundException");} // different String url = "jdbc: mysql: // localhost/LearnJSP"; // establish a connection to java. SQL. connection connection = null; Statement stmt = null; ResultSet rs = null; try {c Onnection = DriverManager. getConnection (url, "root", "011124"); stmt = connection. createStatement (); // SQL statement String SQL = "select * from userinfo where username = '" + sUserName + "' and userpwd = '" + sPasswd + "'"; rs = stmt.exe cuteQuery (SQL); // return the query result} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} try {if (rs. next () // if the record set is not empty, it indicates a matched user name and password. After successful logon, set sUserName to the UserName of the session variable. getAttribute ("UserName") to get the user name. // this can also be used as the basis for determining whether a user logs on or not. getSession (). setAttribute ("UserName", sUserName); response. sendRedirect ("login_success.jsp");} else {// otherwise, the logon fails // response. sendRedirect ("MyJsp. jsp "); response. sendRedirect ("login_failure.jsp") ;}} catch (SQLException e) {// TODO Auto-generated catc H block e. printStackTrace ();} try {if (null! = Rs) {rs. close () ;}if (null! = Stmt) {stmt. close () ;}if (null! = Connection) {connection. close () ;}} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}/ *****/private static final long serialVersionUID = 1L ;}
To make the website run properly, register it in web. xml,
The file content is modified as follows:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>zieckey.login.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping></web-app>
All right, these files can form our logon interface.
Note:
1. File directory format
Login.html, login_success.html, and login_failure.html are stored in the same directory,
LoginServlet. java the file's bytecode file LoginServlet. class is placed under the WEB-INF/classes directory (pay attention to the jar package order)
The directory form of the entire project is:
M:/Tomcat5.5/webapps/JSP_Servlet_JavaBean_Login directory
007-01-18 <DIR> META-INF
007-01-18 <DIR> WEB-INF
007-01-18 1,801 login.html
007-01-18 858 login_failure.jsp
007-01-18 15:40 234 login_success.html
007-01-18 781 MyJsp. jsp
007-01-18 16:12 859 login_success.jsp
M:/Tomcat5.5/webapps/JSP_Servlet_JavaBean_Login/WEB-INF directory
007-01-18 15:16 <DIR> classes
007-01-18 :16 <DIR> lib
007-01-18 606 web. xml
M:/Tomcat5.5/webapps/JSP_Servlet_JavaBean_Login/The WEB-INF/classes/zieckey/login/servlet directory
3,900 LoginServlet. class
2. Other considerations
The MySQL server program must be started first.
I hope this article will help you with JSP program design.