java.lang.NumberFormatException:用于JSP页中的输入字符串

juud5qan  于 2022-12-07  发布在  Java
关注(0)|答案(1)|浏览(127)

你好,有人能帮助为什么在JSP页面中显示值时会出现错误吗?我没有任何数字被转换或字符串被转换为数字,但是我得到了NumberFormatException
我的Servlet获取显示用户记录的请求

if(action.equalsIgnoreCase("update")){
        System.out.println("Came into Update");
        userId=(int) Integer.parseInt(request.getParameter("userid"));
        nbId=request.getParameter("nbId").trim();
        System.out.println("User iD and NBid: "+ userId + nbId);

        User user=new User();
        user.setUser_id(userId);
        user.setUser_nbk(nbId);


        List userRecords=UserDAO.getUserRecord(user);
        request.setAttribute("userRecords", userRecords);
        List owningOrg=Owning_Org_DB.getOwningOrgRecords();
        request.setAttribute("owningOrg", owningOrg);
        request.getRequestDispatcher("WEB-INF/JSP/TableMaintenance/UserNewAdd.jsp").forward(request, response);
  • 你好
@Entity
 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
  public class User {

@Id
private int user_id;
private String user_first_name;
private String user_middle_name;
private String user_last_name;

       //getters and setters methos
   }

DAO:

public static List getUserRecord(User obj){

    Logger lo=LoggerFactory.getLogger("UserDAO.getUserRecord");
    Session session= Annotationsessionfactory.getAnnotationSession();
    Transaction tx=session.beginTransaction();

    lo.debug("Request for A user Record");
    //List<User> recList=new ArrayList<User>();
    List recList=null;
    try{

        //String userRecord="from User";
        Criteria userList=session.createCriteria(User.class)
                .add(Restrictions.eq("user_nbk", obj.getUser_nbk()))
                .add(Restrictions.eq("user_id", obj.getUser_id()));


         recList=userList.list();
        System.out.println(recList.size());

    }catch (Exception e){

        lo.info("Exception Occured in UserDAO.getUserRecord");
        lo.debug("Exception Occured in UserDAO.getUserRecord:"+e);
        tx.rollback();

    }finally{

        session.close();

        lo.info("Session Closed in UserDAO.userRecordslist finally block: ");
    }

    lo.debug("Record was sent to the requesting servlet or method: "+ recList);
    return recList;

}

JSP页面

<table>
 <tr>
  <td>First Name</td>
   <td><input type="text" id="firstname" name="firstname" maxlength="80" value="">${userRecords.user_first_name}</td>
</tr>
 <tr>
 <td>Middle Name</td>
 <td><input type="text" id="middlename" name="middlename" maxlength="80" value="${userRecords.user_middle_name}"></td>

.......

我得到的异常:

SEVERE: Servlet.service() for servlet jsp threw exception
**java.lang.NumberFormatException: For input string: "user_first_name"**
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 javax.el.ListELResolver.coerce(ListELResolver.java:166)
at javax.el.ListELResolver.getValue(ListELResolver.java:51)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
at org.apache.el.parser.AstValue.getValue(AstValue.java:123)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938)
at org.apache.jsp.WEB_002dINF.JSP.TableMaintenance.UserNewAdd_jsp._jspService    (UserNewAdd_jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.servicedbUpdate.UserUpdateDB_NewAdds.doPost(UserUpdateDB_NewAdds.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
at java.lang.Thread.run(Thread.java:619)
    Feb 13, 2013 10:30:49 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet UserUpdateDB_NewAdds threw exception
       **java.lang.NumberFormatException: For input string: "user_first_name"**
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 javax.el.ListELResolver.coerce(ListELResolver.java:166)
at javax.el.ListELResolver.getValue(ListELResolver.java:51)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
at org.apache.el.parser.AstValue.getValue(AstValue.java:123)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938)
at org.apache.jsp.WEB_002dINF.JSP.TableMaintenance.UserNewAdd_jsp._jspService(UserNewAdd_jsp.java:77)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.servicedbUpdate.UserUpdateDB_NewAdds.doPost(UserUpdateDB_NewAdds.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)
at java.lang.Thread.run(Thread.java:619)
q1qsirdb

q1qsirdb1#

这里的${userRecords}

${userRecords.user_first_name}
${userRecords.user_middle_name}

是一个List<User>,但是您试图将它当作单个User来访问。这是无效的。在EL中,List只能用整数索引来访问,该索引指示您要访问的列表项的位置,如下所示

${userRecords[0].user_first_name}
${userRecords[0].user_middle_name}

这个异常也基本上告诉我们它需要的是一个整数值而不是字符串值。如果你仔细观察堆栈跟踪,你会发现涉及到一个ListELResolver
然而,你的具体问题更大。你实际上应该迭代列表。使用JSTL<c:forEach>。例如(简化自你的奇数代码片段):

<table>
    <c:forEach items="${userRecords}" var="user">
        <tr>
            <td>${user.user_first_name}</td>
            <td>${user.user_middle_name}</td>
        </tr>
    </c:forEach>
</table>
  • (注意:如果您真的打算将它们重新显示为输入值,请记住XSS攻击漏洞)*

顺便说一下,我会处理你的Java code conventions。那些下划线真的不是Java风格的。

相关问题