Cookie保存信息时遇到的问题java.lang.IllegalArgumentException

通过Cookie保存信息时遇到java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value问题。

代码:

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CookieExampleServlet
 */
@WebServlet("/CookieExampleServlet")
public class CookieExampleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CookieExampleServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime = sdf.format(new Date());

		String lastVistTime ="";
		int vistedCount = 0;
//		 获取客户端浏览器保存的所有Cookie
		Cookie[] cookies = request.getCookies();
		if (cookies != null)
			for (Cookie cookie : cookies) {
				// 判断是否为记录最近访问时间的Cookie
				if ("lastVistTime".equals(cookie.getName())) {
					lastVistTime = cookie.getValue();
				}
				// 判断是否为记录访问次数的Cookie
				if ("vistedCount".equals(cookie.getName())) {
					vistedCount = Integer.valueOf(cookie.getValue());
				}
			}
		// 若曾经访问过,输出上次访问时间
		if (!"".equals(lastVistTime))
			out.println("您上一次的访问时间是:" + lastVistTime);
		// 输出访问次数
		out.println("您是第" + (vistedCount + 1) + "次访问本网站");
		// 以本次访问时间重建同名新Cookie
		Cookie lastVistTimeC = new Cookie("lastVistTime", nowTime);
		// 设置最大存活时间:一年
		lastVistTimeC.setMaxAge(365 * 24 * 60 * 60);
		// 以新访问次数重建同名新Cookie
		Cookie visitCountC = new Cookie("vistedCount",
				String.valueOf(vistedCount + 1));
		// 设置最大存活时间:一年
		visitCountC.setMaxAge(365 * 24 * 60 * 60);
		// 将上述新建Cookie响应到客户端
		response.addCookie(lastVistTimeC);
		response.addCookie(visitCountC);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
上段代码报错。

原因是在获取当前时间时,格式中出现了空格。

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

"yyyy-MM-dd HH:mm:ss"

将此句中的空格去掉就可以了

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃三明治的胖虎x

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值