Java Web学习--获取Cookie报错An invalid character [32] was present in the Cookie value
2017-07-11 17:39
686 查看
初学Java Web,看的是明日科技的《Java Web从入门到精通》,其中第六章 JSP内置对象 6.2.3节 获取cookie 有一个例子:
通过cookie保存并读取用户登录信息:
index.jsp 代码如下
deal.jsp 代码如下:
在用Eclipse编译运行的时候,报错:
Type Exception Report
Message An exception occurred processing JSP page [/deal.jsp] at line [17]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
Root Cause
求助搜索,了解到 An invalid character [32] was present in the Cookie value 中32对应的编码是空格,Stack Overflow上的回答:This
is due to Tomcat's cookie processing being changed to a RFC 6265 compliant implementation by default in 8.5, which does not allow space (character 32), among others. 我的tomcat版本是9.0,然而知道是空格的原因,但并不清楚是哪里产生了空格,逐条语句分析后,怀疑 new
java.util.Date().toLocaleString()这句中有空格输出,使用控制台打印System.out.println(new java.util.Date().toLocaleString()), 输出值确实有空格,改用新的方式输入时间,避免出现空格。
修改后的deal.jsp代码:
再次运行,输入用户名mr后,正常显示。
结论:println真的好用。
通过cookie保存并读取用户登录信息:
index.jsp 代码如下
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.net.URLDecoder" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>通过cookie保存并读取用户登录信息</title> </head> <body> <% Cookie[] cookies = request.getCookies();//从request中获得Cookie对象的集合 String user = ""; //登录用户 String date = ""; //注册的时间 if (cookies != null) { for (int i = 0; i < cookies.length; i++) { //遍历cookie对象的集合 if (cookies[i].getName().equals("mrCookie")) {//如果cookie对象的名称为mrCookie user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);//获取用户名 date = cookies[i].getValue().split("#")[1];//获取注册时间 } } } if ("".equals(user) && "".equals(date)) {//如果没有注册 %> 游客您好,欢迎您初次光临! <form action="deal.jsp" method="post"> 请输入姓名:<input name="user" type="text" value=""> <input type="submit" value="确定"> </form> <% } else {//已经注册 %> 欢迎[<b><%=user %></b>]再次光临<br> 您注册的时间是:<%=date %> <% } %> </body> </html>
deal.jsp 代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.net.URLEncoder" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>写入cookie</title> <script type="text/javascript">window.location.href="index.jsp"</script> </head> <body> <% request.setCharacterEncoding("utf-8"); String user=URLEncoder.encode(request.getParameter("user"),"utf-8"); //获取用户名 Cookie cookie = new Cookie("mrCookie", user+"#"+new java.util.Date().toLocaleString()); cookie.setMaxAge(60*60*24*30); //设置cookie有效期30天 response.addCookie(cookie); //保存cookie %> </body> </html>
在用Eclipse编译运行的时候,报错:
Type Exception Report
Message An exception occurred processing JSP page [/deal.jsp] at line [17]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing JSP page [/deal.jsp] at line [17] 14: String user=URLEncoder.encode(request.getParameter("user"),"utf-8"); //鑾峰彇鐢ㄦ埛鍚� 15: Cookie cookie = new Cookie("mrCookie", user+"#"+new java.util.Date().toLocaleString()); 16: cookie.setMaxAge(60*60*24*30); //璁剧疆cookie鏈夋晥鏈�30澶� 17: response.addCookie(cookie); //淇濆瓨cookie 18: %> 19: 20: </body> Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:182) org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:115) org.apache.catalina.connector.Response.generateCookieString(Response.java:1010) org.apache.catalina.connector.Response.addCookie(Response.java:962) org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:385) org.apache.jsp.deal_jsp._jspService(deal_jsp.java:126) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
求助搜索,了解到 An invalid character [32] was present in the Cookie value 中32对应的编码是空格,Stack Overflow上的回答:This
is due to Tomcat's cookie processing being changed to a RFC 6265 compliant implementation by default in 8.5, which does not allow space (character 32), among others. 我的tomcat版本是9.0,然而知道是空格的原因,但并不清楚是哪里产生了空格,逐条语句分析后,怀疑 new
java.util.Date().toLocaleString()这句中有空格输出,使用控制台打印System.out.println(new java.util.Date().toLocaleString()), 输出值确实有空格,改用新的方式输入时间,避免出现空格。
修改后的deal.jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.net.URLEncoder,java.util.*,java.text.*" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>写入cookie</title> <script type="text/javascript">window.location.href="index.jsp"</script> <body> <% request.setCharacterEncoding("utf-8"); Date date = new Date(); SimpleDateFormat dtf = new SimpleDateFormat("h:m:s"); String user=URLEncoder.encode(request.getParameter("user"),"utf-8"); //从“user"获取用户名 Cookie cookie = new Cookie("mrCookie", user+"#"+dtf.format(date)); System.out.println(dtf.format(date)); cookie.setMaxAge(60*60*24*30); //设置cookie有效期30天 response.addCookie(cookie); //保存cookie %> </body> </html>
再次运行,输入用户名mr后,正常显示。
结论:println真的好用。
相关文章推荐
- 关于Cookie的An invalid character [32] was present in the Cookie value异常
- java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
- An invalid character [34] was present in the Cookie value] with root cause
- HTTP Status 500 - An invalid character [44] was present in the Cookie value
- java.lang.IllegalArgumentException: An invalid character [44] was present in the Cookie value
- 尝试获取Cookie时出现的错误提示:"A potentially dangerous Request.Cookies value was detected from the client:
- Hibernate异常:query specified join fetching, but the owner of the fetched association was not present in the select list
- CareerCup Find the ceiling value present in the BST of a given ke
- Unable to add data connection The given key was not present in dictionary
- C# The given key was not present in the dictionary.错误原因及解决办法。
- Unity打包报错the given key was not present in the dictionary
- org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the sel
- query specified join fetching, but the owner of the fetched association was not present in the selec
- Dynamics CRM:“the given key was not present in the dictionary”
- 转:The given key was not present in the dictionary.(问题解决方法)
- query specified join fetching, but the owner of the fetched association was not present in the select list
- query specified join fetching, but the owner of the fetched association was not present in the selec
- Read the value of a cookie in Jmeter
- query specified join fetching, but the owner of the fetched association was not present in the selec
- Troubleshoot: The given key was not present in the dictionary