继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JSP模拟登陆页面(连接MySQL数据库)

linbamu
关注TA
已关注
手记 1
粉丝 1
获赞 58
本地数据库名: test_mysql
用户表名:  userinfo
表的结构:
        username--varchar(10)
        password--varchar(20)

<em>注:编码均为UTF-8 。如果要在你的机子上运行以下代码,请将你对应的数据库名、登陆数据库的用户名、密码及表作出相应的修改</em>


index.jsp

<%@ 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
   <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
   <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<style>
    form{
        margin-top:20px;

    }
    input{
        margin: 0 10px 0 10px;
    }
</style>

<title>请先登录</title>
</head>
<body>

    <form class="form-inline text-center" role="form" action="doLogin.jsp" method="post">

        <div class="form-group">
            <label>用户名:<input type="text" class="form-control" name="username"></label>
        </div>

        <div class="form-group">
            <label>密码<input type="password" class="form-control" name="password"></label>
        </div>

        <div  class="form-group">
            <label><input type="submit"  class="form-control btn-primary"  value="提交"></label>
        </div>
    </form>
</body>
</html>
</body>
</html>

doLogin.jsp

<%@page import="java.sql.*"%>
<%@ 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆中</title>
</head>
<body>
    <%!
        String DBDriver = "com.mysql.jdbc.Driver";
        String DBUrl = "jdbc:mysql://localhost:3306/test_mysql?useUnicode=true&useSSL=false";
        String DBUser = "root";
        String DBPass = "1589";
        boolean getPermit = false;

    %>
    <%
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            // 记得反射先
            Class.forName(DBDriver);
            con = DriverManager.getConnection(DBUrl, DBUser, DBPass);
            String sql = "";

            pstmt = con.prepareStatement("select * from userinfo where username=?");    
            pstmt.setString(1, username);
            rs = pstmt.executeQuery();

            // 说明找到记录
            if(rs.next()) {
                if (rs.getString(2).equals(password)) {
                    getPermit = true;
                }
            }
            out.println(rs.getString(2));
        } catch (SQLException e) {
            System.out.println("连接数据库失败");
            e.printStackTrace();

        } finally {
            rs.close();
            pstmt.close();
            con.close(); 
        }
    %>
    <%
        if(getPermit) {
            request.getRequestDispatcher("success.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("failure.jsp").forward(request, response);
        }
    %>

</body>
</html>

failure.jsp

<%@ 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录失败</title>
</head>
<body>
    <center>
        <h2>登录失败<a href="index.jsp">返回</a>重新登录</h2>
    </center>
</body>
</html>

success.jsp

<%@ 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆成功</title>
</head>
<body>
    <center>
        <h2>欢迎用户<%=request.getParameter("username") %>,登陆成功</h2>
    </center>
</body>
</html>
打开App,阅读手记
24人推荐
发表评论
随时随地看视频慕课网APP

热门评论

dologin.jsp中怎么连接数据库的语句都报错,需要设置什么?

格式怪怪dei...

查看全部评论