从String转换为Int时出现JSP异常错误

时间:2013-02-26 12:00:28

标签: java jsp exception

有一个班级为X.Java.在下面这两行我正在做

request.getSession.setAttribute("count",String.valueOf(rsp.getCount()));

在上面我将从rsp.getCount()的数据库中获取数据。这里getcount()是整数 类型,我将更改为字符串String.valueOf(rsp.getCount()并将一个属性设置为count

有一个jsp作为X.jsp 现在我正在使用getAttribute并将值存储在failCount类型的string变量中。

String failCount = request.getSession.getAttribute("count");

现在我想将此值转换为整数类型,因为我写了这行

int countInt = Integer.parseInt(failCount);

我收到的错误如下

127039 13-02-26 17:13:35 ERROR http-10.18.2.105-12205-Processor4 - <JSPFilter ServletException> - test.abc.common.filter.JSPFilter.doFilter(JSPFilter.java:111) 
org.apache.jasper.JasperException: Exception in JSP: abc/x.jsp:66 

63:              
64:          } 
65:          String failCount = (String)request.getSession().getAttribute("failCount"); 
66:          int failCountint = Integer.parseInt(failCount); 
67:          //System.out.println(failCountint); 
68:           
69:           


Stacktrace: 
        at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:489) 
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411) 
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308) 
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259) 
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) 
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) 
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679) 
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461) 
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399) 
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301) 
        at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:142) 
        at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:247) 
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1105) 
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:841) 
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:755) 
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396) 
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:350) 
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:627) 
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

确保您在HttpRequest中设置会话

request.getSession.setAttribute("count",String.valueOf(rsp.getCount()));

这个java文件是servlet然后它会运行良好。

还有一件事要确保rsp.getCount()不返回“null”。

在JSP文件中

String failCount = (String)request.getSession().getAttribute("failCount"); 
int failCountint = Integer.parseInt(failCount); 

为什么你使用request.getSession()而不是session,而你试图获得failcount而不是count

试试这个

String failCount = session.getAttribute("count").toString();
int countInt = Integer.parseInt(failCount);

我认为这可能会有所帮助...

答案 1 :(得分:0)

设定值为会话:

request.getSession.setAttribute("count",String.valueOf(rsp.getCount()));

从会话中读取价值:

String failCount = (String)request.getSession().getAttribute("failCount"); 

在会话中设置值时,您调用它count并在从JSP中读取值时,您将其称为failCount

答案 2 :(得分:0)

我认为failCount为空的原因 int failCountint = Integer.parseInt(failCount);

所以请使用 的System.out.println(故障计数);

并且还在错误行周围使用try catch块。