Using JSP to make the website login interface, and connect the database

Source: Internet
Author: User

here's how to connect to a database and make a simple login interface with JSP

1. first need to set up a good environment, have Tomcat,eslipse,jdk and so on, and configure the environment variables, and install the database, here I use the database is MySQL database, and downloaded a mysql GUI software, when the above preparation work is completed, you can start to do.

2.(1) The database should be connected first

This is the graphical display interface of the database,

(2) after opening, first create a new mysql connection.

(3) then in this interface to set the name of the connection point, there are passwords, passwords need to remember, in the future after the MySQL connection to the database to enter, the other content does not need to be modified, click OK after completion.

(4) Create a new table under the wzw1 of this connection point

(5) The new table will need to fill in the information in the form, because we use the user's user name and password, so set up user and password

(6) Click Add Field

(7) Add a field and then continue filling in the information

(8) Then click Save, enter the name of the table in the popup screen

(9) My new table name is wzw2, open and then see this interface, description, build table Success

In this way, you have completed MySQL 's initial setup for setting up a login interface.

3, Next, open eslipse, create a new project like this

The following interface will appear

The first time you open it, you need to configure Tomcat.

then enter the project name and click Finish to

4. After you have established the project, find the document shown under your Mysql-connector-java and copy it .

Open the created project, where the file name is WebContent under the WEB-INF under the lib file, paste the copied content, so that you can put MySQL is connected to this project as shown in

So the connection is successful.

5. you can then create a new jsp file

Enter the name of the file you want to name and click Finish

Open JSP file will appear the following interface, you need to change charset=iso-8859-1 to charset=utf-8

write A JSP file need to know some html Writing syntax, below is my Code

<% @ page language = "java" contentType = "text / html; charset = utf-8"
    pageEncoding = "utf-8"%>
<! DOCTYPE html PUBLIC "-// W3C // DTD HTML 4.01 Transitional // EN" "">
<meta http-equiv = "Content-Type" content = "text / html; charset = utf-8">

    function $ (id) {
        return document.getElementById (id); // This is to return an object named in id format. For example, if the id in the username tag below is "user", then the user is returned.
        // The returned object can be used by the following check () method
    The function check () {// check () method is used to determine whether to jump to the next jsp interface.
        var user = $ ("user"). value;
        var psd = $ ("psd"). value; // Get user information with user and psd
        $ ("userinfo"). innerHTML = "";
        $ ("psdinfo"). innerHTML = ""; // Use userinfo and psdinfo to write prompts, such as no prompt information such as user name, no password, etc.
        if (user == "") // If the user name is empty, assign the content of userinfo to the following content, then the prompt information can be displayed
                $ ("userinfo"). innerHTML = "The username cannot be empty";
                $ ("user"). focus ();
                return false; // Can't jump to the page at this time, so return false
        if (psd == "")
                $ ("psdinfo"). innerHTML = "Password cannot be empty"; // Same as above
                $ ("psd"). focus ();
                return false;
        return true;
    // script is written in Java, the following is
</ script>

<title> Sign in </ title>
</ head>
<body background = file: /// E: / working directory / myJavaweb / 1.jpg>

    <form action = "loginResultre.jsp" method = "post" onsubmit = "return check ()"> // This tag is used to reference the next jsp file. The reference method is by looking at check (), if it is true, enter Next interface, otherwise stop at this interface.
    <tr> <td> User name </ td> <td> <input name = "userName" id = "user"> <span id = "userinfo"> </ span> </ td> </ tr>
    <tr> <td> password </ td> <td> <input type = "password" name = "userword" id = "psd"> <span id = "psdinfo"> </ span> </ td> </ td> </ td> tr>
    <tr> <td> <input type = "submit" value = "login"> </ td> </ tr>
    </ table>
    </ form>
    </ center>
</ body>
</ html>

<% @ page language = "java" import = "java.sql. *" contentType = "text / html; charset = UTF-8"
    pageEncoding = "UTF-8"%>
<! DOCTYPE html PUBLIC "-// W3C // DTD HTML 4.01 Transitional // EN" "">
<meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8">
<title> Login results </ title>
</ head>
    <% // This "% is used to compile Java content
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null; // Define the link, execution, and results
        String driverName = "com.mysql.jdbc.Driver";
        String userName = "root"; // The name of the database
        String userPwd = "199762"; // Database password
        String dbName = "wzw1"; // The name of the data table
        String url1 = "jdbc: mysql: // localhost: 3306 /" + dbName;
        String url2 = "? User =" + userName + "& password =" + userPwd;
        String url3 = "& useUnicode = true & characterEncoding = UTF-8"; // Specify a good character set
        String url = url1 + url2 + url3; // together the above three pieces of information
        request.setCharacterEncoding ("UTF-8"); // Specify a good character set
        Class.forName (driverName); // Connect to the database
        conn = DriverManager.getConnection (url); // Pass the information over
        String sql = "select * from wzw1 where user =? And password =?"; // Query command
        pstmt = conn.prepareStatement (sql); // Execute
        String user = request.getParameter ("userName"); // Get the information of the previous page
        String password = request.getParameter ("userPwd");
        pstmt.setString (1, user); // Process the two contents
        pstmt.setString (2, password);
        rs = pstmt.executeQuery (); // Query
        if ( ()) {
            %> <center> <h1> User <% = rs.getString ("user")%> Login succeeded! </ h1> </ center>
        else {
            %> <center> <h1> Incorrect username or password! </ h1> </ center>
        if (rs! = null) {
            rs.close ();
        if (pstmt! = null) {
            pstmt.close ();
        if (conn! = null) {
            conn.close (); // Where there is a problem here, just turn off that variable
</ body>
</ html>

Using JSP to make the website login interface, and connect the database

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: 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.