• cookie的使用


    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
      <display-name>apcheck</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>com.checkservlet.logincheck</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login1</url-pattern>
      </servlet-mapping>
      <servlet>
        <servlet-name>servlet</servlet-name>
        <servlet-class>com.checkservlet.mysecondservlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/login2</url-pattern>
      </servlet-mapping>
     
    </web-app>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <style>
    .page{
    height:250px;
    300px;
    background-color: deepskyblue;
    margin: auto;
    }
    .login{
        height: 20px;
        text-align: center;
    }
    .username{
    margin-top: 20px;
    height:50px;
    margin-left: 20px;
    }
    .pwd{
    height:70px;
    margin-left: 20px;
    }
    .commit{
        margin-left: 20px;
    height:50px;
    }
    </style>
    <%
        String username = "";
        String password = "";
        //获取当前站点的所有Cookie
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {//对cookies中的数据进行遍历,找到用户名、密码的数据
            if ("username".equals(cookies[i].getName())) {
                username = cookies[i].getValue();
            } else if ("password".equals(cookies[i].getName())) {
                password = cookies[i].getValue();
            }
        }
    %>
    </head>
    <body>
    <div class="page">
    <form action="./login2" method="POST">
        <div class="login">登录</div>
            <div class="username">
                用户名:<input type="text" name="username" value="<%=username%>"><br/>
            </div>
            <div class="pwd">
                密   码:<input type="password" name="password" value="<%=password%>" > <br/>
                <input type="checkbox" value="y" name="isLogin">下次自动登录<br/>
            </div>
            <div class="commit">
                <button type="submit" class="btnSubmit" style="">登录</button>
            </div>
    </form>
    </div>
    </body>
    </html>
    login.jsp
    package com.checkservlet;
    
    import java.io.IOException;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    
    /**
     * Servlet implementation class mysecondservlet
     */
    @WebServlet("/mysecondservlet")
    public class mysecondservlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public mysecondservlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            String username = request.getParameter("username");    
            String password = request.getParameter("password");    
            String flag = request.getParameter("isLogin");
            String url = "jdbc:mysql://localhost:3306/webdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true";
            
            String user="root";
            String pwd="123";
            Connection conn=null;
            Statement stmt=null;
            ResultSet rs=null;
            int count=0;
            String cmd = "select * from user where username='"+username+"' and password='"+password+"'";  
    
            try {  
                Class.forName("com.mysql.jdbc.Driver"); 
                conn = (Connection) DriverManager.getConnection(url,user,pwd); 
                stmt = (Statement) conn.createStatement(); 
      
                rs = stmt.executeQuery(cmd); 
                rs.last(); 
                count=rs.getRow();  
                System.out.println("共有" + rs.getRow() + "行记录:");  
                rs.beforeFirst(); 
                
                } catch (ClassNotFoundException e) {  
                    System.out.println("加载驱动异常");  
                    e.printStackTrace();  
                } catch (SQLException e) {  
                    System.out.println("数据库异常");  
                    e.printStackTrace();  
                } finally {  
                    try {  
                        if (rs != null)                         
                                rs.close(); 
                         if (stmt != null) 
                                stmt.close(); 
                        if (conn != null)  
                            conn.close(); 
                    } catch (SQLException e) {  
                        e.printStackTrace();  
                    }  
                }  
                  
               
            if(count!=0){  
                if ("y".equals(flag)) {
                    //创建两个Cookie对象
                    Cookie nameCookie = new Cookie("username", username);
                    //设置Cookie的有效期为30天
                    nameCookie.setMaxAge(60 * 60 * 24 * 30);
                    //nameCookie.setPath("/WEB0001/login.jsp");
                    Cookie pwdCookie = new Cookie("password", password);
                    pwdCookie.setMaxAge(60 * 60 * 24 * 30);
                    //pwdCookie.setPath("/WEB0001/login.jsp");
                    response.addCookie(nameCookie);
                    response.addCookie(pwdCookie);
                } 
                response.getWriter().write("Welcome!"+username+","+"Your Password:"+password);
            }
            else{
                //用户登录失败
                response.getWriter().write("Sorry!your username or password is wrong!");
            }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    mysecondservlet
  • 相关阅读:
    这种人就是傻逼
    WinDBG + VMWare 双机调试
    最近我遇到了一个代码上的问题
    GITHUB,Fork别人的代码,然后更新的问题
    又半个月没写了,最近忙,真的忙,在考虑换工作的问题
    最近在弄clamav,这里把clamav的编译方法弄一下吧
    基于seay代码,加了个小功能
    CTF:第七题
    CTF:第六题
    Python str拼接bytes
  • 原文地址:https://www.cnblogs.com/hechunhang/p/10654787.html
Copyright © 2020-2023  润新知