• jsp第六次作业


    实现JSp与数据库连接的登录

    ====数据库

    CREATE TABLE user (
        userid VARCHAR(30) PRIMARY KEY,
        name VARCHAR(30) NOT NULL,
        password VARCHAR(32) NOT NULL
    );
    INSERT INTO user(userid, name, password) VALUES('admin', 'administrator','admin');

    login.jsp界面:

    <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
    <html>
    <head>
    <title>www.school.com</title>
    </head>
    <body>
        
            <h1>登录操作</h1>
            <hr>
            <form action="login_check.jsp" method="post">
                <table border="1">
                    <tr>
                        <td colspan="2">用户登录</td>
                    </tr>
                    <tr>
                        <td>登录ID:</td>
                        <td><input type="text" name="id"></td>
                    </tr>
                    <tr>
                        <td>登录密码:</td>
                        <td><input type="password" name="password"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" value="登录"> <input
                            type="reset" value="重置"></td>
                    </tr>
                </table>
            </form>
        
    </body>
    </html>

    login_check.jsp界面:(实现数据库的连接及操作)

    <%@page import="java.nio.channels.SeekableByteChannel"%>
    <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    <html>
    <head>
    <title>www.school.com</title>
    </head>
    <body>
        <%!public static final String DBDRIVER = "com.mysql.jdbc.Driver";
        public static final String DBURL = "jdbc:mysql://localhost:3306/jsp";
        public static final String DBUSER = "root";
        public static final String DBPASS = "root";%>
        <%
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            boolean flag = false;
            String name = null;
        %>
        <%
            try {
                Class.forName(DBDRIVER);
                conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
                String sql = "SELECT name FROM user WHERE userid=? AND password=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, request.getParameter("id"));
                pstmt.setString(2, request.getParameter("password"));
            
                rs = pstmt.executeQuery();
                if (rs.next()) {
                    name = rs.getString(1);
                    flag = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (rs != null)
                        rs.close();
                    if (pstmt != null)
                        pstmt.close();
                    if (conn != null)
                        conn.close();
                    if (flag) {
                        request.getRequestDispatcher("login_success.jsp").forward(request, response);
                    } else {
                        response.sendRedirect("login_failure.jsp");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        %>
    
    </body>
    </html>

    login_success.jsp界面:(登录成功界面)

    <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
    <html>
    <head>
        <title>www.school.com</title>
    </head>
    <body>
    
        <h1>登录操作</h1><hr>
        <h2>登录成功</h2>
        <h2>欢迎<font color="red">
      
        </font>光临!</h2>
    
    </body>
    </html>

    login_failure.jsp界面:(失败界面)

    <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
    <html>
    <head>
        <title>www.school.com</title>
    </head>
    <body>
    
        <h1>登录失败</h1><hr>
        <h2>登录失败,请重新<a href="login.jsp">登录</a>!</h2>
    
    </body>
    </html>

     

     

  • 相关阅读:
    MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
    MyBatis学习总结(五)——实现关联表查询
    MyBatis学习总结(六)——调用存储过程
    MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突
    MyBatis学习总结(二)----使用MyBatis对表执行CRUD操作
    MyBatis学习总结(三)——优化MyBatis配置文件中的配置
    CORS跨域问题
    按指定后缀名删除文件的demo
    Markdown起步
    Linux和git起步
  • 原文地址:https://www.cnblogs.com/hyonf/p/14641722.html
Copyright © 2020-2023  润新知