在用Cookie保存用户名时候,当用户名是中文的时候服务器报错了。
HTTP Status 500 - An exception occurred processing JSP page /dologin.jsp at line 40
type Exception report
message An exception occurred processing JSP page /dologin.jsp at line 40
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 /dologin.jsp at line 40 37: Cookie passwordCookie = new Cookie("password",password); 38: usernameCookie.setMaxAge(86400); 39: passwordCookie.setMaxAge(86400); 40: response.addCookie(usernameCookie); 41: response.addCookie(passwordCookie); 42: }else 43: { Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470) 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:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.IllegalArgumentException: Control character in cookie value or attribute. org.apache.tomcat.util.http.CookieSupport.isHttpSeparator(CookieSupport.java:193) org.apache.tomcat.util.http.CookieSupport.isHttpToken(CookieSupport.java:217) org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:186) org.apache.catalina.connector.Response.generateCookieString(Response.java:1070) org.apache.catalina.connector.Response.addCookie(Response.java:1012) org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:380) org.apache.jsp.dologin_jsp._jspService(dologin_jsp.java:107) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 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:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.
------------------------------------------------------------------------------分割线------------------------------------------------------------------------------
这里设置request.setCharacterEncoding("utf-8");也改变不了报错
解决方法:Java网络包里面编码和解码的工具类(URLEncoder)
原来的代码
String username = request.getParameter("username"); String password = request.getParameter("password"); Cookie usernameCookie = new Cookie("username",username); Cookie passwordCookie = new Cookie("password",password); usernameCookie.setMaxAge(86400); passwordCookie.setMaxAge(86400); response.addCookie(usernameCookie); response.addCookie(passwordCookie);
修改代码
用URLEncoder.encode解决无法在Cookie当中保存中文字符串的问题
String username = URLEncoder.encode(request.getParameter("username"),"utf-8") ; String password = URLEncoder.encode(request.getParameter("password"),"utf-8"); Cookie usernameCookie = new Cookie("username",username); Cookie passwordCookie = new Cookie("password",password); usernameCookie.setMaxAge(86400); passwordCookie.setMaxAge(86400); response.addCookie(usernameCookie); response.addCookie(passwordCookie);
然后还要解码
URLDecoder.decode
注意: 编码解码分别在java.net.URLDecoder 和 java.net.URLEncoder下。记得导入类