jsp里怎么写登录界面

2019年5月15日11:51:12 发表评论 2 浏览

此次试验所用到的软件是myeclipse10,tomcat7,Dreamweaver,sqlserver2008数据库。可以实现用户使用用户名和密码登录。

推荐课程:Java教程。

jsp里怎么写登录界面

如果登录成功,页面会显示登录成功,如果密码错误,则页面会显示登录失败。连接数据库使用的事javabean方法,需要实现下载好sqlserver2008的驱动程序,在web project文件夹下的src文件夹下新建包“Bean”,并在此包下新建“DBBean.java”文件。

DBBean.java文件代码如下:

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) {
        }
    }
}

在WEBROOT目录下有三个jsp页面文件:分别是login.jsp,logincheck.jsp,loginsuccess.jsp.在login.jsp页面中,可以通过输入用户名、密码,点击登录按钮,实现登录成功loginsucccess.jsp页面的跳转,如果密码错误,则页面会跳转到登录失败的页面。(当然,在进行页面跳转之前,需要在sqlserver2008中新建一个数据库,在数据库目录下新建一个表,并填入表的信息)

文件夹结构截图:

jsp里怎么写登录界面

login.jsp登录界面代码:

<%@ page import="java.sql.*" 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录界面</title>
</head>
<body>
    <center>
        <h1 style="color:red">登录</h1>
            <form id="indexform" name="indexForm" action="logincheck.jsp" method="post">
                <table border="0">
                    <tr>
                        <td>账号:</td>
                        <td><input type="text" name="username"></td>
                    </tr>
                    <tr>
                        <td>密码:</td>
                        <td><input type="password" name="password">
                        </td>
                    </tr>
                </table>
            <br>
                <input type="submit" value="登录" style="color:#BC8F8F">
            </form>
            <form action="zhuce.jsp">
                <input type="submit" value="注册" style="color:#BC8F8F">
            </form>
    </center>
</body>
</html>

indexcheck.jsp登录失败代码:

<%@ page import="java.sql.*" 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="db" class="Bean.DBBean" scope="page" />
<%
    request.setCharacterEncoding("UTF-8");
    String username=(String)request.getParameter("username");
    String password=(String)request.getParameter("password");//取出login.jsp的值
    
    //下面是数据库操作
    String sql="select * from login where username="+"'"+username+"'";//定义一个查询语句
    ResultSet rs=db.executeQuery(sql);//运行上面的语句
    if(rs.next())
    {
        /* if(password.equals(rs.getString(2)))
        {
            
        } */
        if(password.equals(rs.getObject("password"))){
            response.sendRedirect("loginsuccess.jsp");
        }
        else{
            out.print("<script language='javaScript'> alert('密码错误');</script>");
            response.setHeader("refresh", "0;url=login.jsp");
        }
    }
    else 
    {
        out.print("<script language='javaScript'> alert('账号错误——else');</script>");
        response.setHeader("refresh", "0;url=login.jsp");
    }
    
%>
</body>
</html>

indexsuccess.jsp登录成功代码:

<%@ page import="java.sql.*" 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>登陆成功</h1>
</body>
</html>

最终的页面效果如下:

jsp里怎么写登录界面

如果全都正确,则显示如下页面:

jsp里怎么写登录界面

如果密码错误,则显示如下页面:

jsp里怎么写登录界面

以上就是jsp里怎么写登录界面的详细内容,更多请关注洋洋博客其它相关文章!

jsp里怎么写登录界面

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: