Demo_JSP of architecture instance, demo_jsp

Source: Internet
Author: User

Demo_JSP of architecture instance, demo_jsp

Demo_JSP of architecture instance

1,Development tools and development environment

Development tools: MyEclipse10, JDK1.6.0 _ 13 (32-bit), Tomcat7.0 (32-bit), and mysql5.7.13

Development Environment: WIN10

2,Demo_JSP implementation function

User Logon, user registration, and logout.

3,Demo_JSP Usage Technology

This example uses JSP and JDBC to Implement User Logon, user registration, and logout. Figure 1 shows the system architecture:

Figure 1: Demo_JSP System Architecture

See figure 2 (Logical Relationship Diagram between JSP files in Demo_JSP ):

Figure 2: Logical Relationship Between JSP files in Demo_JSP

 

4,Implementation

(1) create a new Web project in MyEclipse and name it Demo_JSP;

(2) Import mysql-connector-java-5.1.6-bin.jar to Demo_JSP project, this package is to achieve Java Connection database function package (will not import the package of students, can Baidu yo );

Attachment: mysql-connector-java-5.1.6-bin.jar, Baidu cloud download link: http://pan.baidu.com/s/1i5psdDF password: meyg

(3) create the following JSP file in the Demo_JSP project:

1) login. jsp: The homepage page for user logon. The 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) login_action.jsp receives the username and password entered by the user on the login. jsp page and implements logon authentication through JDBC. The specific code is as follows:

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%><%@ include file="inc.jsp"%><%//get parametersString username = request.getParameter("username");String password = request.getParameter("password");//check nullif (username == null || password == null) {    response.sendRedirect("login.jsp");}//validateboolean isValid = false;String sql = "select * from userInfo where username='"+username+"' and password='"+password+"'";try {    Class.forName(drv).newInstance();    Connection conn = DriverManager.getConnection(url, usr, pwd);    Statement stm = conn.createStatement();    ResultSet rs = stm.executeQuery(sql);    if(rs.next())isValid = true;    rs.close();    stm.close();    conn.close();} catch (Exception e) {    e.printStackTrace();    out.println(e);} finally {}if (isValid) {    session.setAttribute("username", username);    response.sendRedirect("welcome.jsp");} else {    response.sendRedirect("login.jsp");}%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

3) inc. jsp, which stores the database connection address. The specific code is as follows:

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%><%String drv = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/library_system";String usr = "root";String pwd = "root";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

4) welcome. jsp: the main interface after successful logon. The 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"> 

5) loginout. jsp: log out and return to the logon page. The Code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%session.removeAttribute("username");response.sendRedirect("login.jsp");%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

6) register. jsp implements the user registration interface. The specific 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"> 

7) register_action.jsp, implements registration through JDBC, and writes data to the database. The specific code is as follows:

<%@ include file="inc.jsp"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%//get parametersString username = request.getParameter("username");String password1 = request.getParameter("password1");String password2 = request.getParameter("password2");String email = request.getParameter("email");//check nullif (username == null || password1 == null || password2 == null || !password1.equals(password2)) {    response.sendRedirect("register.jsp");}//validateboolean isValid = false;String sql = "select * from userInfo where username='"+username+"'";try {    Class.forName(drv).newInstance();    Connection conn = DriverManager.getConnection(url, usr, pwd);    Statement stm = conn.createStatement();    ResultSet rs = stm.executeQuery(sql);    if(!rs.next()) {        sql = "insert into userInfo(username,password,mail) values('"+username+"','"+password1+"','"+email+"')";        stm.execute(sql);        isValid = true;    }        rs.close();    stm.close();    conn.close();} catch (Exception e) {    e.printStackTrace();    out.println(e);}if (isValid) {    response.sendRedirect("login.jsp");} else {    response.sendRedirect("register.jsp");}%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

5,Running result display

(1) Enter http: // localhost: 8080/Demo_JSP/login. jsp in the browser, as shown in Figure 3:

 Figure 3: logon page

(2) Click register a new user, as shown in figure 4:

 Figure 4: registration page

 

(3) After successful registration, the system will automatically return to the logon page, enter the user name and password, and click logon, as shown in Figure 5:

 

Figure 5: welcome. jsp page after Logon

 

Attachment: Demo_JSP project source code Baidu cloud download link: http://pan.baidu.com/s/1mifI8nI password: j3wp; this instance uses the database to Create Table SQL statement File Download link: http://pan.baidu.com/s/1eS0n9aM password: 7ttd

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.