zoukankan      html  css  js  c++  java
  • 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>

     

     

  • 相关阅读:
    npm install报错Error: ENOENT
    hibernate hql
    mysql授权
    撤销git reset soft head操作
    nodejs&npm等概念梳理
    Java类中中文问题
    采用MANIFEST.MF之jar报错ClassNotFoundException解法
    EChart和G2比较
    牛客OI测试赛1
    Codeforces Round #340 (Div.2)
  • 原文地址:https://www.cnblogs.com/hyonf/p/14641722.html
Copyright © 2011-2022 走看看