Java Web学习--获取Cookie报错An invalid character [32] was present in the Cookie value

初学Java Web,看的是明日科技的《Java Web从入门到精通》,其中第六章 JSP内置对象 6.2.3节 获取cookie 有一个例子:

通过cookie保存并读取用户登录信息:

index.jsp 代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>




通过cookie保存并读取用户登录信息


<%
	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)) {//如果没有注册
%>
		游客您好,欢迎您初次光临!
		
请输入姓名:
<% } else {//已经注册 %> 欢迎[<%=user %>]再次光临
您注册的时间是:<%=date %> <% } %>

deal.jsp 代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.net.URLEncoder" %>



写入cookie



<%
	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
%>


在用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: 


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.*" %>



写入cookie
 

<%
	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
%>



再次运行,输入用户名mr后,正常显示。

Java Web学习--获取Cookie报错An invalid character [32] was present in the Cookie value_第1张图片

结论:println真的好用。


你可能感兴趣的:(学习记录)