SetCookies@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
for(int i=0;i<3;i++){
Cookie c = new Cookie("str001:"+i, "str0011:"+i);
System.out.println("setCookie"+i);
resp.addCookie(c);
Cookie cc = new Cookie("str002:"+i, "str0022:"+i);
cc.setMaxAge(1000);
System.out.println("setAgeCookie"+i);
resp.addCookie(cc);
}
//resp.setCharacterEncoding("GBK");
resp.setContentType("text/html;charset=GBK");
PrintWriter pw = resp.getWriter();
pw.println("<html><head><title>设置Cookies</title></head></html>"+
"<body>设置6个Cookie\n+" +
"<a href=\"ShowCookies\">"+
"查看</a></body>");
}ShowCookiespublic class ShowCookies extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
PrintWriter pw = resp.getWriter();
Cookie[] cookies = req.getCookies();
if(cookies!=null){
Cookie c ;
for(int i=0;i<cookies.length;i++){
c = cookies[i];
pw.println(c.getValue());
}
}
}getName 可以打出来 不过str001后面的冒号打不出来为什么?
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
for(int i=0;i<3;i++){
Cookie c = new Cookie("str001:"+i, "str0011:"+i);
System.out.println("setCookie"+i);
resp.addCookie(c);
Cookie cc = new Cookie("str002:"+i, "str0022:"+i);
cc.setMaxAge(1000);
System.out.println("setAgeCookie"+i);
resp.addCookie(cc);
}
//resp.setCharacterEncoding("GBK");
resp.setContentType("text/html;charset=GBK");
PrintWriter pw = resp.getWriter();
pw.println("<html><head><title>设置Cookies</title></head></html>"+
"<body>设置6个Cookie\n+" +
"<a href=\"ShowCookies\">"+
"查看</a></body>");
}ShowCookiespublic class ShowCookies extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
PrintWriter pw = resp.getWriter();
Cookie[] cookies = req.getCookies();
if(cookies!=null){
Cookie c ;
for(int i=0;i<cookies.length;i++){
c = cookies[i];
pw.println(c.getValue());
}
}
}getName 可以打出来 不过str001后面的冒号打不出来为什么?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货