第六周

<%--
  Created by IntelliJ IDEA.
  User: Dell
  Date: 2021/4/11
  Time: 21:44
  To change this template use File | Settings | File Templates.
--%>
<%@ 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="text" name="inputVcode"/><img src="/WebProject_war_exploded/createCode"><br/>
    <input type="submit" value="登录">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
     
    <title>My JSP 'dologin.jsp' starting page</title>
     
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
 
  </head>
   
  <body>
   
    <%
        String Name = request.getParameter("user");  //获取输入的用户名
        String ps = request.getParameter("password");  // 密码
        String code = request.getParameter("code");   //验证码
        Name.trim();  //去空格
        ps.trim();
         
         
        PreparedStatement pre = null;
        Connection con =null;
        Statement sql;
        ResultSet rs; 
        request.setCharacterEncoding("utf-8");
         
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(Exception e){
            out.print("<h1>加载错误"+e);
        }
         
         
        String user = "root";
        String password="root";
     
         
        con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test",user,password);
             
             
        try{
            sql = con.createStatement();
             
            String SQLL = "select * from user where uname=? and upwd=?";
            pre=con.prepareStatement(SQLL);
            pre.setString(1,Name);
            pre.setString(2,ps);
             
             
            rs = pre.executeQuery();
             
            if(rs.next()){
                String na = rs.getString(2);
                session.setAttribute("uname",na);
                response.sendRedirect("welcome.jsp");
                 
                 
            }else{
            %>
            <script >         //弹窗提示
            alert('输入密码或用户名错误');
            </script>
            <%
                response.sendRedirect("index.jsp");
            }          
        }catch(SQLException e){
            out.print("<h1>查询错误"+e);
        }
         
             
         
        %>
   </body>
</html>
<<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
     
    <title>My JSP 'welcome.jsp' starting page</title>
     
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
 
  </head>
   
  <body>
    欢迎<%=session.getAttribute("uname") %></body>
</html>

原文地址:https://www.cnblogs.com/zhangbowen123/p/14645728.html