• jsp第六周作业


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

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

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

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

    //连接数据库

     1 package session;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.PreparedStatement;
     6 import java.sql.ResultSet;
     7 import java.sql.SQLException;
     8 
     9 
    10 public class Datalink {
    11 
    12 
    13     //获取连接
    14     protected Connection getConnection(){
    15         Connection conn=null;
    16             try {
    17                 Class.forName("com.mysql.jdbc.Driver");
    18                 // 2.建立连接
    19                 conn = DriverManager.getConnection(
    20                         "jdbc:mysql://localhost:3306/mysql", "root", "root");
    21             } catch (Exception e) {
    22                 e.printStackTrace();
    23             } 
    24             return conn;
    25     }    
    26     
    27 
    28     //关闭连接
    29     protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
    30     try {
    31         if(rs != null)
    32             rs.close();
    33         if(ps != null)
    34             ps.close();
    35         if(con != null)
    36             con.close();
    37         
    38         } catch (SQLException e) {
    39             e.printStackTrace();
    40         }
    41     }
    42     
    43 }

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     3 <html>
     4   <head>
     5     <title>用户登录</title>
     6   </head>
     7   <body>
     8         <form name="form1" method="post" action="dologin.jsp">
     9             用户名:
    10             <input type="text" name="userName">
    11             密码:
    12             <input type="password" name="pwd">
    13             <input type="submit" value="登录">
    14         </form>
    15   </body>
    16 </html>
     1 <%@ page contentType="text/html;charset=UTF-8" language="java"%>
     2 <html>
     3 <head>
     4 <title>Title</title>
     5 </head>
     6 <body>
     7     <%
     8         String uname = request.getParameter("username");
     9         String upwd = request.getParameter("password");
    10 
    11         String realUname = "";
    12         String reaslPassword = "";
    13 
    14         Class.forName("com.mysql.jdbc.Driver");
    15 
    16         Connection connection = DriverManager.getConnection(
    17                 "jdbc:mysql://localhost:3306/mysql", "root", "root");
    18 
    19         PreparedStatement preparedStatement = connection
    20                 .prepareStatement("SELECT * FROM dl where uname = ?");
    21         preparedStatement.setString(1, uname);
    22 
    23         ResultSet rs = null;
    24         try {
    25             rs = preparedStatement.executeQuery();
    26         } catch (SQLException e) {
    27             e.printStackTrace();
    28         }
    29 
    30         while (rs.next()) {
    31             realUname = rs.getString("uname");
    32             reaslPassword = rs.getString("upwd");
    33         }
    34 
    35         if (uname.equals(realUname) && upwd.equals(reaslPassword)
    36                 && inputVcode.equals(inputVcode)) {
    37             HttpSession httpSession = request.getSession();
    38             httpSession.setAttribute("username", uname);
    39             httpSession.setAttribute("password", upwd);
    40             response.sendRedirect("welcome.jsp");
    41         } else {
    42             response.sendRedirect("index.jsp");
    43         }
    44     %>
    45 </body>
    46 </html>
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     3 <html>
     4 <head>
     5 <title>session</title>
     6 </head>
     7 
     8 <body>
     9     <%
    10         String uname = (String) session.getAttribute("uname");
    11         if (uname == null) {
    12             response.sendRedirect("index.jsp");
    13         }
    14     %>
    15     欢迎您!<%=uname%>
    16 </body>
    17 </html>

  • 相关阅读:
    Linux安装Gradle
    MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)
    Websocket实现即时通讯
    Java线程池的使用
    Html5视频播放器-VideoJS+Audio标签实现视频,音频及字幕同步播放
    几种常用的认证机制
    Spring 接口参数加密传输
    Java 三种方式实现接口校验
    Spring AOP实现 Bean字段合法性校验
    RabbitMQ进程结构分析与性能调优
  • 原文地址:https://www.cnblogs.com/wuhaoovo/p/14644244.html
Copyright © 2020-2023  润新知