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

Cookie-02

2019-11-09 18:11:59
字体:
来源:转载
供稿:网友

package com.blm.controller;

import java.io.IOException; import java.io.PRintWriter; import java.net.URLDecoder;

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 SecondServlet extends HttpServlet {

@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); PrintWriter out=resp.getWriter(); Cookie[] cook=req.getCookies(); if(cook==null) { out.write("第一次访问,welcome"); } else { for(int i=0;i<cook.length;++i) { if(cook[i].getName().equals("loginInfo")) { System.out.println("当前登录用户信息:"+URLDecoder.decode(cook[i].getValue(), "UTF-8")); out.write("当前登录用户信息:"+URLDecoder.decode(cook[i].getValue(), "UTF-8")); } } } }@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp);}

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表