Java 如何在jsp中将对象类型转换为int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20113023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to typecast object to int in jsp
提问by user2532973
I need to be able to compare the value that is in an object to an int, but I can't seem to get the typecasting working. I have tried all the methods described here: Session attribute access and converting to int?but they all give me a null pointer exception. Here is the code I have that's not working.
我需要能够将对象中的值与 int 进行比较,但我似乎无法进行类型转换。我已经尝试了这里描述的所有方法:Session attribute access and Conversion to int? 但它们都给了我一个空指针异常。这是我无法使用的代码。
Here is the code that puts values into the session.
这是将值放入会话的代码。
<script>sessionStorage.clickcount=1;</script>
<input type ="hidden" name="ttmp" value="222" id="ttmp" />
<script>document.getElementById('ttmp').value = sessionStorage.clickcount;</script>
This was tried with the input line before and after the script.
这是在脚本之前和之后的输入行中尝试过的。
<%
String value = request.getParameter("ttmp");
if (value != null) {
session.setAttribute("test", value);
}
Integer userid = Integer.parseInt(session.getAttribute("test").toString());
...
if (userid == 1) {
...
}
%>
回答by SpringLearner
try this Integer.parseInt((String)session.getAttribute("test"))
尝试这个 Integer.parseInt((String)session.getAttribute("test"))
<%
String value = request.getParameter("ttmp");
if (value != null) {
session.setAttribute("test", value);
}
Integer userid = Integer.parseInt((String)session.getAttribute("test"));
...
if (userid == 1) {
...
}
%>
回答by Ali Baig
Try this now.
现在试试这个。
<%
String value = request.getParameter("ttmp");
if (value != null) {
session.setAttribute("test", value);
}
int userid=0;
if ((session.getAttribute("test") != null) && (session.getAttribute("test") != ""))
{
userid = Integer.parseInt(session.getAttribute("test").toString());
}
%>