• JSP第六次课后作业 2021/04/07


    1.安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据

    2.制作jsp登录页面 login.jsp 提交到dologin.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在

    3.如果存在,把用户名保存在SESSION中,跳转到welcome.jsp,welcome.jsp中读取session中的用户名,显示欢迎你xxx

    4.若不存在,跳到登录页面。

    MySQL数据库内的数据信息

    package com.school.lty;
    
    import java.sql.DriverManager;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import com.mysql.jdbc.Driver;
    
    public class JDBCKu {
    
        public static Connection getConnection() throws Exception {
            //1.获取Driver实现类的对象
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
    
            //2.提供另外三个连接的基本信息
            String url = "jdbc:mysql://localhost:3306/user_info?useUnicode=true&characterEncoding=utf8";
            String user="root";
            String password = "root";
    
            //注册驱动
            DriverManager.registerDriver(driver);
    
            //获取连接
            Connection conn = DriverManager.getConnection(url,user,password);
            return conn;
        }
    
        // 关闭连接
        public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) throws Exception {
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (conn != null)
                conn.close();
        }
    
    }
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
      </head>
    
      <body>
      <form action="doLogin.jsp" method="post" >
        用户名:<input type="text" name="username"/>
        <br>
        密码:<input type="password"  name="password"/>
        <br>
        <input type="submit" value="登录">
      </form>
      </body>
    </html>
    <%@ page import="java.sql.Connection" %>
    <%@ page import="static com.school.lty.JDBCKu.getConnection" %>
    <%@ page import="java.sql.PreparedStatement" %>
    <%@ page import="java.sql.ResultSet" %>
    <%@ page import="com.school.lty.BaseDao" %>
    <%@ page import="com.school.lty.JDBCKu" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    
    <%
        JDBCKu jdbcKu = new JDBCKu();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
    
        Connection conn = jdbcKu.getConnection();
        String sql = "select * from user where uname = ? and upassword = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setNString(1,username);
        ps.setNString(2,password);
    
        ResultSet rs = ps.executeQuery();
    
        if (rs.next() == true) {
            request.getSession().setAttribute("username",username);
            request.getRequestDispatcher("welcome.jsp").forward(request,response);
        }else{
            response.sendRedirect("index.jsp");
        }
    
        jdbcKu.closeAll(conn, ps, rs);
    %>
    <body>
    
    </body>
    </html>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
    <%
        String username = (String) request.getSession().getAttribute("username");
    %>
    欢迎您。。。<%=username%>
    </body>
    </html>

  • 相关阅读:
    【HDU
    写个shell脚本依次运行每个程序半小时
    Windows10 + Visual Studio 2017环境为C++工程安装使用ZMQ
    【UVALive
    【Gym
    【最短路算法】Dijkstra+heap和SPFA的区别
    【Gym 100812C】Story of Princess (走完图所有边)
    【C++】VS2015/VS2017连接Mysql数据库教程
    博客园设置Google-code-prettify渲染代码高亮
    【QML与C++混合编程】用QVariantList传递数组类型成员
  • 原文地址:https://www.cnblogs.com/lilbetter03/p/14635396.html
Copyright © 2020-2023  润新知