首页 > 学院 > 开发设计 > 正文

Cookie

2019-11-09 18:12:09
字体:
来源:转载
供稿:网友
package com.blm.controller;import java.io.IOException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 设置cookie * @author Administrator * */public class FirstServlet extends HttpServlet { @Override PRotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); //1、创建一个cookie(在服务器端创建的) //多个值存储到一个cookie中,把多个值用一个特殊字符连接起来, //(特殊字符就是为了截取字符),然后用一个键来存储连接的字符串. String value = URLEncoder.encode("张三,zhangsan1231", "UTF-8"); Cookie c=new Cookie("loginInfo", value); //不设置的话,则cookies不写入硬盘,而是写在内存, //只在当前页面有用,以秒为单位,单位是秒 c.setMaxAge(2*60);//2分钟 c.setPath("/cookiedemo/part"); //2、将一个cookie添加到客户端 resp.addCookie(c); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表