zoukankan      html  css  js  c++  java
  • 入门:JavaWeb Cookie

    总结:

      JavaWeb 利用Cookie 存储在本地用户名和密码,设置Cookie的生存时间。

      两个页面,一个登陆页面,一个登陆后的页面,在登陆页面选择是否保存Cookie(保存Cookie,下次自动填充用户名和密码),提价后根据用户的选择在第二个页面进行Cookie的设置。

    登陆页面:

     1 import java.io.IOException;
     2 import java.io.PrintWriter;
     3 
     4 import javax.servlet.ServletException;
     5 import javax.servlet.annotation.WebServlet;
     6 import javax.servlet.http.Cookie;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 @WebServlet("/JavaWebcs")
    12 public class JavaWebcs extends HttpServlet {
    13     private static final long serialVersionUID = 1L;
    14 
    15     public JavaWebcs() {
    16         super();
    17     }
    18 
    19     protected void doGet(HttpServletRequest request,
    20             HttpServletResponse response) throws ServletException, IOException {
    21         response.setContentType("text/html;charset=utf-8");
    22         PrintWriter out = response.getWriter();
    23         String userName = "";
    24         String password = "";
    25         String ischecked = "";
    26         Cookie[] list = request.getCookies();
    27         if (list != null && list.length > 0) {
    28             ischecked = "checked";
    29             for (int i = 0; i < list.length; i++) {
    30                 if (list[i].getName().equals("username"))
    31                     userName = list[i].getValue();
    32                 if (list[i].getName().equals("password"))
    33                     password = list[i].getValue();
    34             }
    35         }
    36         out.println("<html><head><meta charset='utf-8'><title>Login!</title></head></body>"
    37                 + "<form action='yz' method='POST'>user name:<input name='username' type='text' value="
    38                 + userName
    39                 + "><br />user pwd:<input name='password' type='password' value="
    40                 + password
    41                 + "><br />save user info<input name='check' type='checkbox' value='Yes'"
    42                 + ischecked
    43                 + "><br /><input type='button' onclick='submit()' value='submit'></form>"
    44                 + "</body></html>");
    45     }
    46 
    47     protected void doPost(HttpServletRequest request,
    48             HttpServletResponse response) throws ServletException, IOException {
    49         doGet(request, response);
    50     }
    51 
    52 }

    跳转到的页面(设置Cookie):hello username

     1 import java.io.IOException;
     2 import java.io.PrintWriter;
     3 
     4 import javax.servlet.ServletException;
     5 import javax.servlet.annotation.WebServlet;
     6 import javax.servlet.http.Cookie;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 @WebServlet("/yz")
    12 public class yz extends HttpServlet {
    13     private static final long serialVersionUID = 1L;
    14 
    15     public yz() {
    16         super();
    17     }
    18 
    19     protected void doGet(HttpServletRequest request,
    20             HttpServletResponse response) throws ServletException, IOException {
    21         PrintWriter out = response.getWriter();
    22         String username = request.getParameter("username");
    23         String password = request.getParameter("password");
    24         Cookie userName = new Cookie("username", username);
    25         Cookie passWord = new Cookie("password", password);
    26         if (request.getParameter("check") != null
    27                 && request.getParameter("check").equals("Yes")) {
    28             userName.setMaxAge(7 * 24 * 60 * 60);
    29             passWord.setMaxAge(7 * 24 * 60 * 60);
    30         } else {
    31             userName.setMaxAge(0);
    32             passWord.setMaxAge(0);
    33         }
    34         response.addCookie(userName);
    35         response.addCookie(passWord);
    36         out.println("hello " + username);
    37 
    38     }
    39 
    40     protected void doPost(HttpServletRequest request,
    41             HttpServletResponse response) throws ServletException, IOException {
    42         doGet(request, response);
    43     }
    44 
    45 }
  • 相关阅读:
    http://www.jdon.com/jivejdon/thread/37340
    我的英语死在类似的问题上
    Linux之read命令使用
    SIP注册呼叫流程简介
    sh里的变量 $0 $1 $$ $#
    LTE 逻辑分层和接口协议
    LTE语音业务VOLTE
    shell编程——if语句 if z n f eq ne lt
    高通QXDM抓modem log
    LTE与VOLTE基础知识
  • 原文地址:https://www.cnblogs.com/A--Q/p/5890121.html
Copyright © 2011-2022 走看看