JSP enables user logon, registration, and exit. jsp User Logon

Source: Internet
Author: User

JSP enables user logon, registration, and exit. jsp User Logon

This article describes how to use JSP to Implement User Logon, including user logon, registration, and logout.

1. system use case diagram

2. Page Flowchart

3. Database Design
This example uses the oracle database

Create user table

Including id, username, password, and email. There are four fields in total.

-- Create table create table P_USER (id VARCHAR2 (50) not null, username VARCHAR2 (20), password VARCHAR2 (20), email VARCHAR2 (50 )) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage (initial 64 minextents 1 maxextents unlimited); -- Add comments to the table comment on table P_USER is 'user table '; -- Add comments to the columns comment on column P_USER.id is 'id'; comment on column P_USER.username is 'username'; comment on column P_USER.password is 'Password '; comment on column P_USER.email is 'email ';

4. Page Design
4.1 logon page
Login. jsp

<% @ 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"> 

Page Effect

3.2 logon logic processing page
Login_action.jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" java. SQL. * "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <% String username = request. getParameter ("username"); String password = request. getParameter ("password"); if (username = null | "". equals (username. tri M () | password = null | "". equals (password. trim () {// out. write ("the user name or password cannot be blank! "); System. out. println (" the user name or password cannot be blank! "); Response. sendRedirect ("login. jsp "); return; // request. getRequestDispatcher ("login. jsp "). forward (request, response);} boolean isValid = false; Connection con = null; // create a database Connection PreparedStatement pre = null; // create a pre-compiled statement object, this is generally used instead of Statement ResultSet result = null; // create a result set object try {Class. forName ("oracle. jdbc. driver. oracleDriver "); // load the Oracle driver // System. out. println ("start to try to connect to the database! "); String url =" jdbc: oracle: "+" thin: @ 127.0.0.1: 1521: orcl "; // 127.0.0.1 is the local address, orcl is the default Oracle Database Name String user = "scott"; // user name, the default system account name String pwd = "tiger "; // select the SET Password con = DriverManager during installation. getConnection (url, user, pwd); // get the connection // System. out. println ("connection successful! "); String SQL =" select * from p_user where username =? And password =? "; // Pre-compiled statement,"?" Pre = con. prepareStatement (SQL); // instantiate the pre-compiled statement pre. setString (1, username); // set the parameter. The first 1 indicates the index of the parameter, rather than the index pre of the column name in the table. setString (2, password); // set the parameter. The first 1 indicates the index of the parameter, rather than the index result of the column name in the table = pre.exe cuteQuery (); // execute the query, note that the parameter if (result. next () {isValid = true ;}} catch (Exception e) {e. printStackTrace ();} finally {try {// close the above objects one by one, because not closing will affect performance and occupy resources. // pay attention to the order of disabling, the last use first closes if (result! = Null) result. close (); if (pre! = Null) pre. close (); if (con! = Null) con. close (); // System. out. println ("the database connection is closed! ");} Catch (Exception e) {e. printStackTrace () ;}} if (isValid) {System. out. println (" Logon successful! "); Session. setAttribute (" username ", username); response. sendRedirect (" welcome. jsp "); return;} else {System. out. println (" Logon Failed! "); Response. sendRedirect (" login. jsp "); return ;}%>

Use JDBC to connect to the database. If the user name or password is empty, you can still jump to the login. jsp page.
If the user name and password are not empty, connect to the database to query the user table. If the user record can be queried, the logon is successful. Save the user information to the session and go to the welcome page.

If no log is found based on the user name and password, the logon fails. log on to the login. jsp page again.

3.3 welcome page
Welcome. jsp

<% @ 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"> 

Use EL expressions to display user information
Effect


3.4 Welcome Page exit logic processing page
Loginout. jsp

<%@ 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"> 

Remove the session user information to go To the login. jsp logon page.
3.5 registration page
Register. jsp

<% @ 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"> 

When you click "register" on the logon page, the user registration page is displayed.
Effect

3.6 registration logic processing page
Register_action.jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" java. SQL. * "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <% String username = request. getParameter ("username"); String password1 = request. getParameter ("password1"); String password2 = request. getParameter ("Password2"); String email = request. getParameter ("email"); if (username = null | "". equals (username. trim () | password1 = null | "". equals (password1.trim () | password2 = null | "". equals (password2.trim () |! Password1.equals (password2) {// out. write ("the user name or password cannot be blank! "); System. out. println (" the user name or password cannot be blank! "); Response. sendRedirect ("register. jsp "); return; // request. getRequestDispatcher ("login. jsp "). forward (request, response);} boolean isValid = false; Connection con = null; // create a database Connection PreparedStatement pre = null; // create a pre-compiled statement object, this is generally used instead of Statement ResultSet result = null; // create a result set object try {Class. forName ("oracle. jdbc. driver. oracleDriver "); // load the Oracle driver // System. out. println ("start to try to connect to the database! "); String url =" jdbc: oracle: "+" thin: @ 127.0.0.1: 1521: orcl "; // 127.0.0.1 is the local address, orcl is the default Oracle Database Name String user = "scott"; // user name, the default system account name String pwd = "tiger "; // select the SET Password con = DriverManager during installation. getConnection (url, user, pwd); // get the connection // System. out. println ("connection successful! "); String SQL =" select * from p_user where username =? "; // Pre-compiled statement,"?" Pre = con. prepareStatement (SQL); // instantiate the pre-compiled statement pre. setString (1, username); // set the parameter. The first 1 indicates the index of the parameter, rather than the index result of the column name in the table = pre.exe cuteQuery (); // execute the query, note that the parameter if (! Result. next () {SQL = "insert into p_user (id, username, password, email) values (?,?,?,?) "; // Pre-compiled statement,"?" Pre = con. prepareStatement (SQL); // instantiate the pre-compiled statement pre. setString (1, System. currentTimeMillis () + ""); // sets the parameter. The first 1 indicates the index of the parameter, rather than the index pre of the column name in the table. setString (2, username); // set the parameter. The first 1 indicates the index of the parameter, rather than the index pre of the column name in the table. setString (3, password1); // sets the parameter. The first 1 indicates the index of the parameter, rather than the index pre of the column name in the table. setString (4, email); // set the parameter. The first 1 indicates the index of the parameter, rather than the index pre.exe cuteUpdate () of the column name in the table; // execute isValid = true ;}} catch (Exception e) {e. printStackTrace ();} finally {Try {// close the above objects one by one, because if it is not closed, it will affect the performance and occupy resources. // pay attention to the order of closing, and the first to close if (result! = Null) result. close (); if (pre! = Null) pre. close (); if (con! = Null) con. close (); // System. out. println ("the database connection is closed! ");} Catch (Exception e) {e. printStackTrace () ;}} if (isValid) {System. out. println (" registered successfully, please log on! "); Response. sendRedirect (" login. jsp "); return;} else {System. out. println (" the user name already exists! "); Response. sendRedirect (" register. jsp "); return ;}%>

First, determine whether the user name and password are empty, and whether the password is consistent with the password. If the above conditions are not met, return to the registration page register. jsp
If the preceding conditions are true, You can query the database based on the user name. If you can query the records, the user name already exists and the registration page register. jsp is returned.

If no record is found, this user name can be used for registration. One record is inserted into the user table using JDBC, and then the login. jsp page is displayed.

4. Summary
In this example, JSP is used to implement user logon. Two minor issues are encountered during the compilation process.

4.1 After the query, determine whether the record exists and use if (! Result. next (), rather than the while LOOP used in the common query, this should be noted, especially when processing registration.

4.2 JSP page compilation Error.

When using return in a JSP script, you must be cautious. compilation errors may occur.

The solution is to use only the JSP script on the JSP homepage to ensure that no compilation is required after return.

The above is a brief introduction to using JSP to Implement User Login. I hope it will be helpful for you to learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.