JSP连数据库登录检查用户名和密码模板

版权声明:© 2018 • OmegaXYZ-版权所有 转载请注明出处 - QQ:644327005 https://blog.csdn.net/xyisv/article/details/86359431

JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML(标准通用标记语言的子集)文件(.htm,.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件,后缀名为(*.jsp)。 用JSP开发的Web应用是跨平台的,既能在Linux下运行,也能在其他操作系统上运行。

下面给出了JSP连数据库登录检查用户名和密码模板

登录页代码index.jsp:

注意这里的css要自己哦,要不然太丑了。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>文档管理系统-登录</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
</head>
<body>
<div class="main">
    <div class="login">
        <h1>文档管理系统</h1>
        <div class="inset">
            <!--start-main-->
            <form id="form1" name="form1" method="post" action="loginCheck.jsp">
                <div>
                    <h2>用户登录</h2>
                    <span><label>用户名</label></span>
                    <span><input type="text" class="textbox" name="username" id="username" placeholder="请输入用户名"></span>
                </div>
                <div>
                    <span><label>密码</label></span>
                    <span><input type="text" class="password" name="password" placeholder="请输入密码"></span>
                </div>
                <div class="sign">
                    <input type="submit" value="登录" class="submit" name="Submit" />
                </div>
            </form>

        </div>
    </div>
    <!--//end-main-->
</div>
</body>
</html>
<!--徐奕:开源协议:MIT License (MIT) Copyright © 2018 -->

密码检查代码loginCheck.jsp

<%@ page import="java.sql.*" %>
<body>
<%
    String username=new String(request.getParameter("username").getBytes("ISO8859_1"),"GBK");
    String password=new String(request.getParameter("password").getBytes("ISO8859_1"),"GBK");
    try {
        // 加载数据库驱动,注册到驱动管理器
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        // 数据库连接字符串
        String url = "jdbc:sqlserver://localhost:1433;databaseName=t_customer";
        // 数据库用户名
        String usename = "xy";
        // 数据库密码
        String psw = "123456";
        // 创建Connection连接

        Connection conn = DriverManager.getConnection(url,usename,psw);
        // 判断数据库连接是否为空
        if(conn != null){
            String sql="select * from t_customer where name='"+username+"' and password='"+ password + "'";
            Statement stmt = conn.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
            if(rs.next()){
                application.setAttribute("name",username);
                String description = rs.getString("description");
                application.setAttribute("description",description);
                response.sendRedirect("frame.jsp");
            }else{
                response.sendRedirect("loginFailed.jsp");
%>
<a href="javascript:history.back()">返回</a>
<%
            }
            // 输出连接信息
            //out.println("数据库连接成功!");
            // 关闭数据库连接
            conn.close();
        }else{
            // 输出连接信息
            System.out.println("数据库连接失败!");
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
%>
</body>

登录失败代码loginFailed.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="refresh" content="3;url=index.jsp">
    <title>登录失败</title>
    <script type="text/javascript">
        function info() {
//循环执行,1秒执行一次
            window.setInterval("daojishi()", 1000);
        }
        function daojishi() {
            if(document.getElementById("msg").innerHTML!=1){
                document.getElementById("msg").innerHTML=document.getElementById("msg").innerHTML-1;
            }
        }
    </script>
</head>
<body "info()">
用户名或密码错误,<span id="msg">3</span>秒返回登录页面
</body>
</html>

更多内容访问omegaxyz.com
网站所有代码采用Apache 2.0授权
网站文章采用知识共享许可协议BY-NC-SA4.0授权
© 2019 • OmegaXYZ-版权所有 转载请注明出处

猜你喜欢

转载自blog.csdn.net/xyisv/article/details/86359431