首页 > 其他 > 详细

解决Cookie乱码问题

时间:2016-09-16 22:50:58      阅读:186      评论:0      收藏:0      [点我收藏+]

写了一个cookie的定义和获取,结果我输入中文后,页面报错

报错信息如下:

type Exception report

message An exception occurred processing JSP page /demo1/page3.jsp at line 16

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: An exception occurred processing JSP page /demo1/page3.jsp at line 16

13:     </form>
14:     <%
15:         Cookie cookie=new Cookie("message","涓枃");
16:         response.addCookie(cookie);
17:      %>
18: </body>
19: </html>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:575)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)


root cause 

java.lang.IllegalArgumentException: Control character in cookie value or attribute.
    org.apache.tomcat.util.http.CookieSupport.isHttpSeparator(CookieSupport.java:205)
    org.apache.tomcat.util.http.CookieSupport.isHttpToken(CookieSupport.java:229)
    org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:185)
    org.apache.catalina.connector.Response.generateCookieString(Response.java:1054)
    org.apache.catalina.connector.Response.addCookie(Response.java:996)
    org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:380)
    org.apache.jsp.demo1.page3_jsp._jspService(page3_jsp.java:97)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)

解决方案:cookie也要转码的

定义一个cookie:

 1 <%@page import="java.net.URLEncoder"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>page3</title>
 9 </head>
10 <body>
11     <form action="page3.jsp">
12         <input type="text" name="text" value=""/>
13         <input type="submit" name="提交"/>
14     </form>
15     <%
16     //转码
17         Cookie cookie=new Cookie("message",URLEncoder.encode("中文", "utf-8"));
18         response.addCookie(cookie);
19      %>
20 </body>
21 </html>

page4.jsp(获取cookie)

 1 <%@page import="java.net.URLDecoder"%>
 2 <%@page import="java.net.URLEncoder"%>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>page4</title>
10 </head>
11 <body>
12 <%
13     String text = request.getParameter("text");
14     out.print("text------------->"+text);
15     Cookie[] cookies=request.getCookies();
16     if(cookies!=null){
17     for(Cookie cookie:cookies){
18         if(cookie.getName().equals("message")){
19         //cookie解码
20             out.print("cookie value------>"+URLDecoder.decode(cookie.getValue(), "utf-8"));
21         }
22     }
23  %>
24  
25 </body>
26 </html>

 

解决Cookie乱码问题

原文:http://www.cnblogs.com/sincoolvip/p/5877412.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!