Create a simple logon interface instance Using JSP and a jsp logon interface instance

Source: Internet
Author: User

Create a simple logon interface instance Using JSP and a jsp logon interface instance

Now many web projects can use the logon interface. This article describes how to create a simple login interface using JSP and share it with you, as shown below:

Running Environment

For details about eclipse + tomcat + MySQL, refer to the Jsp runtime environment -- Tomcat

Project List

Here, I first put the jsp file for access outside Web-INF.

1. Several files to be created are shown in. jsp.

2. Also import the jar package mysql-5.0.5.jar of MySQL, export to the lib folder in the WEB-INF can do not need Bulid Path

3. Start coding:

Code Demonstration:

Index. jsp is like the homepage of a General website. It introduces header. jsp and footer. jsp into it.

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Header. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <div id =" nav "> <ul> <li> <a href =" "> Navigation 1 </a> </ li> <a href = ""> navigation 2 </a> </li> <a href = ""> navigation 3 </a> </ li> <a href = ""> navigation 4 </a> </li> <a href = ""> Navigation 5 </a> </ li> <a href = ""> Navigation 6 </a> </li> </ul> </div>

Footer. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <div> <p> xxxxxxxxxxx can try | xxxxxxxxxxxx Technology Co., Ltd. </p> <p> Beijing ICP Certificate No. 1234567 | Copyright©1999-2017, All Rights Reserved </p> </div>

Page content display:

Login. jsp login user name and password filling Interface

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Content display:

Test. jsp compares the submitted content of the form login. jsp with the data in the database, and then jumps accordingly.

<% @ Page import = "java. SQL. * "%> <% @ page language =" java "contentType =" text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% // request to get login. string username = request. getParameter ("username"); // request to obtain login. the value of jsp password String password = request. getParameter ("password"); // database MySQL address String DBURL = "jdbc: mysql: // localhost: 3306/zhou? UseUnicode = true & characterEncoding = UTF-8 "; String DBName =" root "; // login username String DBPwd =" 123456 "; // login password // load the mysql Driver Class. forName ("com. mysql. jdbc. driver "); // Connection to the database conn = DriverManager. getConnection (DBURL, DBName, DBPwd); // create the Statement object Statement st = conn. createStatement (); // SQL statement, search whether the username and password exist in the Database. String SQL = "select * from user where name = '" + username + "' and pwd = '" + password + "'"; // run the SQL statement and obtain The resulting result is placed in the ResultSet In the result set ResultSet rs1_st.exe cuteQuery (SQL); // you can determine whether the result set exists. Generally, username has only one if (rs. next () {// set a username and assign the content of the subsequent username to the previous username so that the request can be used on the next page. setAttribute ("username", username); // jump to the userpage. jsp request. getRequestDispatcher ("userpage. jsp "). forward (request, response);} else {// set an error and assign the following word to this error so that you can use it to jump to the page first. The request has limited scope. setAttribute ("error", "incorrect user name or password !!! "); Request. getRequestDispatcher (" login. jsp "). forward (request, response) ;}conn. close (); rs. close (); %>

Page content displayed due to logon error:

Userpage. jsp this page is displayed after successful login

<% @ Page import = "javafx. scene. chart. pieChart. data "%> <% @ page import =" java. util. date "%> <% @ page language =" java "contentType =" text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN "" http://www.w3.org/TR/html4/loose.dtd "> <Html> 

Page Content: localhost is 127.0.0.1. Sometimes, when the address bar is local host, eight zeros are displayed:

The whole simple login is complete.

For details about the EL language, refer to the EL expression in this JSP.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.