servlet之cookie

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 java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

/**
 * Created by leo on 17-7-14.
 */
/*
Cookie 是存储在客户端计算机上的文本文件,并保留了各种跟踪信息。Java Servlet 显然支持 HTTP Cookie。
识别返回用户包括三个步骤:
服务器脚本向浏览器发送一组 Cookie。例如:姓名、年龄或识别号码等。
浏览器将这些信息存储在本地计算机上,以备将来使用。
当下一次浏览器向 Web 服务器发送任何请求时,浏览器会把这些 Cookie 信息发送到服务器,服务器将使用这些信息来识别用户。

Servlet Cookie 处理需要对中文进行编码与解码,方法如下:
String   str   =   java.net.URLEncoder.encode("中文","UTF-8");            //编码
String   str   =   java.net.URLDecoder.decode("编码后的字符串","UTF-8");   // 解码
 */
@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {


    private static final long serialVersionUID = 9072647170248270425L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        Cookie name = new Cookie("name", URLEncoder.encode(request.getParameter("name"), "UTF-8"));
        Cookie password = new Cookie("psssword", URLEncoder.encode(request.getParameter("password"), "UTF-8"));
        String printNmae = URLEncoder.encode(request.getParameter("name"), "UTF-8");
        System.out.println(printNmae);

        name.setMaxAge(60 * 5);
        password.setMaxAge(60 * 5);
        response.addCookie(name);
        response.addCookie(password);

        PrintWriter out = response.getWriter();
        out.println("set two cookies. name = " + name + ",password = " + password + "printName = " + printNmae);
    }
}
/*
序号	方法 & 描述
1	public void setDomain(String pattern)
该方法设置 cookie 适用的域,例如 w3cschool.cc。
2	public String getDomain()
该方法获取 cookie 适用的域,例如 w3cschool.cc。
3	public void setMaxAge(int expiry)
该方法设置 cookie 过期的时间(以秒为单位)。如果不这样设置,cookie 只会在当前 session 会话中持续有效。
4	public int getMaxAge()
该方法返回 cookie 的最大生存周期(以秒为单位),默认情况下,-1 表示 cookie 将持续下去,直到浏览器关闭。
5	public String getName()
该方法返回 cookie 的名称。名称在创建后不能改变。
6	public void setValue(String newValue)
该方法设置与 cookie 关联的值。
7	public String getValue()
该方法获取与 cookie 关联的值。
8	public void setPath(String uri)
该方法设置 cookie 适用的路径。如果您不指定路径,与当前页面相同目录下的(包括子目录下的)所有 URL 都会返回 cookie。
9	public String getPath()
该方法获取 cookie 适用的路径。
10	public void setSecure(boolean flag)
该方法设置布尔值,表示 cookie 是否应该只在加密的(即 SSL)连接上发送。
11	public void setComment(String purpose)
设置cookie的注释。该注释在浏览器向用户呈现 cookie 时非常有用。
12	public String getComment()
获取 cookie 的注释,如果 cookie 没有注释则返回 null。
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值