Home  >  Article  >  Java  >  How to write login interface in jsp

How to write login interface in jsp

(*-*)浩
(*-*)浩Original
2019-05-15 11:51:1215381browse

The software used in this experiment is myeclipse10, tomcat7, Dreamweaver, sqlserver2008 database. Users can log in using their username and password.

Recommended course: Java Tutorial.

How to write login interface in jsp

#If the login is successful, the page will display the login success. If the password is incorrect, the page will display the login failure. To connect to the database using the javabean method, you need to download the sqlserver2008 driver, create a new package "Bean" in the src folder under the web project folder, and create a new "DBBean.java" file under this package.

DBBean.java file code is as follows:

package Bean;
import java.sql.*;
public class DBBean {
    private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=JXP";
    private String dbusername = "sa";
    private String dbpassword = "123456";
    private Connection conn = null;
    private Statement stmt = null;

    public DBBean()
    {
        try
        {
            Class.forName(driverStr);
            conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
            stmt = conn.createStatement();
        } 
        catch (Exception ex) {
            System.out.println("数据连接失败!");
        } 
        
    }

    public int executeUpdate(String s) {
        int result = 0;
        System.out.println("--更新语句:"+s+"\n");
        try {
            result = stmt.executeUpdate(s);
        } catch (Exception ex) {
            System.out.println("执行更新错误!");
        }
        return result;
    }

    public ResultSet executeQuery(String s) {
        ResultSet rs = null;
        System.out.print("--查询语句:"+s+"\n");
        try {
            rs = stmt.executeQuery(s);
        } catch (Exception ex) {
            System.out.println("ִ执行查询错误!");
        }
        return rs;
    }
    public void execQuery(String s){
        try {
            stmt.executeUpdate(s);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("执行插入错误!");
        }
    }

    public void close() {
        try {
            stmt.close();
            conn.close();
        } catch (Exception e) {
        }
    }
}

There are three jsp page files in the WEBROOT directory: login.jsp, logincheck.jsp, loginsuccess.jsp. In the login.jsp page, you can enter the user name and password and click the login button to jump to the loginsucccess.jsp page if the login is successful. If the password is incorrect, the page will jump to the failed login page. (Of course, before jumping to the page, you need to create a new database in sqlserver2008, create a new table in the database directory, and fill in the table information)

Screenshot of the folder structure:

How to write login interface in jsp

login.jsp login interface code:

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<meta>
<title>登录界面</title>


    <center>
        <h1>登录</h1>
            <form>
                <table>
                    <tr>
                        <td>账号:</td>
                        <td><input></td>
                    </tr>
                    <tr>
                        <td>密码:</td>
                        <td>
<input>
                        </td>
                    </tr>
                </table>
            <br>
                <input>
            </form>
            <form>
                <input>
            </form>
    </center>

indexcheck.jsp login failure code:

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<meta>
<title>Insert title here</title>


<usebean></usebean>
 alert('密码错误');");
            response.setHeader("refresh", "0;url=login.jsp");
        }
    }
    else 
    {
        out.print("<script> alert(&#39;账号错误——else&#39;);</script>");
        response.setHeader("refresh", "0;url=login.jsp");
    }
    
%>

indexsuccess.jsp login success code:

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<meta>
<title>Insert title here</title>


<h1>登陆成功</h1>

The final page effect is as follows:

How to write login interface in jsp

If everything is correct, the following page will be displayed:

How to write login interface in jsp

If the password is incorrect, the following page will be displayed:

How to write login interface in jsp

The above is the detailed content of How to write login interface in jsp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is action in jspNext article:What is action in jsp