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);}}
新闻热点
疑难解答