zoukankan      html  css  js  c++  java
  • jsp第六周作业

    <%--
      Created by IntelliJ IDEA.
      User: Dell
      Date: 2021/4/10
      Time: 14:34
      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>
    

      dologin.jsp

    <%@ page import="java.sql.*" %><%--
      Created by IntelliJ IDEA.
      User: Dell
      Date: 2021/4/10
      Time: 14:38
      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>
    <%
        String uname = request.getParameter("username");
        String upwd = request.getParameter("password");
        String inputVcode = request.getParameter("inputVcode").toLowerCase();
        String realInputVcode =(String) session.getAttribute("codes");
    
        String realUname="";
        String reaslPassword = "";
    
        Class.forName("com.mysql.jdbc.Driver");
    
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
    
        PreparedStatement preparedStatement =connection.prepareStatement("SELECT * FROM dl where uname = ?");
        preparedStatement.setString(1,uname);
    
    
        ResultSet rs = null;
        try {
            rs = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //如果有数据,rs.next()返回true
        while(rs.next()){
           realUname = rs.getString("uname");
           reaslPassword = rs.getString("upwd");
        }
    
        if(uname.equals(realUname)&&upwd.equals(reaslPassword)&&inputVcode.equals(inputVcode)){
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("username",uname);
            httpSession.setAttribute("password",upwd);
            response.sendRedirect("success.jsp");
        }else {
            response.sendRedirect("login.jsp");
        }
    %>
    </body>
    </html>
    

      success.jsp

    <%--
      Created by IntelliJ IDEA.
      User: Dell
      Date: 2021/4/10
      Time: 15:20
      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>
    <%
        String username =(String) session.getAttribute("username");
    
        if(username==null){
            response.sendRedirect("login.jsp");
        }
    %>
    欢迎你<%=username %>
    </body>
    </html>
    package com.dx.yzm;
    
    import cn.dsna.util.images.ValidateCode;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @WebServlet(name = "CreateCodeController",value = "/createCode")
    public class CreateCodeController extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1.创建验证码图片
            ValidateCode code = new ValidateCode(200,30,4,20);
            String codes = code.getCode();
            HttpSession session = request.getSession();
            session.setAttribute("codes",codes);
            //2.验证码图片响应给客户端
            code.write(response.getOutputStream());
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }
    

      

  • 相关阅读:
    ( ) 与 { } 差在哪?-- Shell十三问<第七问>
    exec 跟 source 差在哪?-- Shell十三问<第六问>
    var=value?export前后差在哪?-- Shell十三问<第五问>
    " "( 双引号) 与 ' '( 单引号) 差在哪?-- Shell十三问<第四问>
    别人 echo 、你也 echo ,是问 echo 知多少?-- Shell十三问<第三问>
    Shell prompt(PS1) 与 Carriage Return(CR) 的关系?-- Shell十三问<第二问>
    Shell十三问更新总结版 -- 什么叫做 Shell?-- Shell十三问<第一问>
    公司项目部运维应急预案-强烈建议收藏!
    抗DDOS应急预案实践-生产环境总结-建议必看
    Ceph日常运维管理和排错 -- <7>
  • 原文地址:https://www.cnblogs.com/s2me/p/14648462.html
Copyright © 2011-2022 走看看