【问题标题】:Number Format Exception JSP Page with cookies带有 cookie 的数字格式异常 JSP 页面
【发布时间】:2016-04-17 21:20:00
【问题描述】:

我写了这个小代码来处理cookies。但是,当我点击“下一步”按钮时,它会抛出一个
java.lang.NumberFormatException: For input string: "current"

代码:

<%@page import="java.util.*"%>
<%
int current=0;
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
if(cookies != null){
    for(int i=0;i<cookies.length;i++){
        if(cookies[i].getName().equals("last"))
            cookie = cookies[i];    
        }
}

if(cookie != null){
    String btn = request.getParameter("button");
            if(btn != null){
                if(btn.equals("next"))
                    current = Integer.parseInt(cookie.getValue()) + 1;
                else
                    current = Integer.parseInt(cookie.getValue()) - 1;
            }
}
response.addCookie(new Cookie("last",String.valueOf("current")));
out.println(current);
%>
<html>
<head>
</head>

<body>
   <form method="post">    
       <input type="submit" name="button" value="prev" />
       <input type="submit" name="button" value="next" />
     </form>
</body>

</html>

【问题讨论】:

    标签: java jsp cookies


    【解决方案1】:

    您将 cookie 设置为值 "current"

    String.valueOf("current")
    

    但你想要

    String.valueOf(current)
    

    【讨论】:

      猜你喜欢
      • 2013-02-19
      • 2013-01-03
      • 1970-01-01
      • 2015-09-19
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多