gpt4 book ai didi

java - 带有 cookie 的数字格式异常 JSP 页面

转载 作者:行者123 更新时间:2023-12-01 10:02:53 24 4
gpt4 key购买 nike

我编写了这段小代码来处理 cookie。但是,当我单击“下一步”按钮时,它会抛出
java.lang.NumberFormatException:对于输入字符串:“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>

最佳答案

您将 cookie 设置为值“current”

String.valueOf("current")

但你想要

String.valueOf(current)

关于java - 带有 cookie 的数字格式异常 JSP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36677039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com