zoukankan      html  css  js  c++  java
  • 使用Cookie记住用户名和密码

    Login.jsp

    <form name = "f1" method="get" action="servlet/LoginServlet">
        <table>
            <tr>
                <td>Username:</td>
                <td><input type="text" name="username" value="${un}"></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name = "password" value="${pw}"></td>
            </tr>
            <tr>
                <td>AutoLogin:</td>
                <td><input type="checkbox" name = "auto" value = "1"></td>
            </tr>    
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Login"> </td>
            </tr>
        </table>
        
        </form>

    PrepareLogin

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Cookie[] cs = request.getCookies();
            
            
            if(cs!=null && cs.length>0){
                for(int i = 0 ; i < cs.length ; i++){
                    Cookie c = cs[i];
                    System.out.println(cs.length);
                    System.out.println(i+"--"+cs[i].getName());
                    if(c.getName().equals("username")){                    
                        String value = c.getValue();
                        request.setAttribute("un", value);
                        System.out.println(value);
                    }
                    if(c.getName().equals("password")){
                        String value = c.getValue() ;
                        request.setAttribute("pw", value);    //将值传递给jsp页面
                        System.out.println(value);
                    }
                }
            }
            
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    保存Cookie

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String auto = request.getParameter("auto"); String username = request.getParameter("username"); String password = request.getParameter("password"); if(auto != null && auto.equals("1")){ Cookie u = new Cookie("username", username); Cookie p = new Cookie("password",password); u.setMaxAge(60*60); p.setMaxAge(60*60); response.addCookie(u); response.addCookie(p); } PrintWriter w = response.getWriter(); w.println("
    <html>"+ "Hello"+ "</html>" ); w.flush(); w.close(); }
  • 相关阅读:
    JZOJ Contest2633 总结
    6813. 【2020.10.05提高组模拟】送信
    HDU 1506 最大子矩形
    2020.10.07【NOIP提高A组】模拟 总结
    6815. 【2020.10.06提高组模拟】树的重心
    2020.10.06【NOIP提高A组】模拟 总结
    2020.10.05【NOIP提高A组】模拟 总结
    gmoj 3976. 【NOI2015模拟1.17】⑨
    2020.09.26【省选组】模拟 总结
    2020.09.19【省选组】模拟 总结
  • 原文地址:https://www.cnblogs.com/da-peng/p/5761772.html
Copyright © 2011-2022 走看看