求助!第一次点击商品存入cookie中的内容是一串奇怪的字符串

来源:8-8 使用Cookie实现保存商品浏览记录

qq_Pinocchio_1

2019-12-10 19:09

运行时报错:

java.lang.NumberFormatException: For input string: "BB318C07B1B138D5500ECA16365941764"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:447)

at java.lang.Integer.parseInt(Integer.java:497)

at dao.ItemsDAO.getViewList(ItemsDAO.java:151)

at org.apache.jsp.details_jsp._jspService(details_jsp.java:221)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:444)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)

at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1137)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)

at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:619)


通过输出cookie中的内容发现第一次点击商品时,存入cookie中的值是形如"BB318C07B1B138D5500ECA16365941764"的字符串,所以在执行Integer.parseInt(arr[i])时会抛出异常。

但是多点击几个商品,查看cookie中保存的内容又是正确的:"BB318C07B1B138D5500ECA16365941764#1#4#2#2#2#1".

导致点击商品次数超过5次才不会报错(因为不会将第一个神奇的字符串转换成数字了)

太奇怪了!为什么第一次点击商品时存入cookie中的值会是个字符串呢???

写回答 关注

1回答

  • 慕无忌6513609
    2020-05-07 11:55:45

    感觉是默认编码不对,你这16进制的

JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248277 学习 · 3071 问题

查看课程

相似问题