Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NumberFormatException: For input string in JSP Page

Hi can someone please help why this is giving an error when trying to display the values in a JSP page. I don't have any number been converted or String been converted to Number, however I get NumberFormatException

my Servlet getting the request to display a user record

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);

POJO:

 @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 Page

<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>

.......

Exception I get:

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)
like image 401
VBJ Avatar asked Feb 13 '13 16:02

VBJ


1 Answers

The ${userRecords} here

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

is a List<User>, however you're attempting to access it as if it's a single User. This is not valid. In EL, a List can only be accessed with an integer index, indicating the position of the list item you'd like to access, like so

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

The exception is also basically telling that it expected an integer value instead of a string value. If you look closer at the stack trace, you'll see that a ListELResolver is involved.

However, your concrete problem is bigger. You should actually be iterating over the list. Use the JSTL <c:forEach> for that. E.g. (simplified from your odd code snippet):

<table>
    <c:forEach items="${userRecords}" var="user">
        <tr>
            <td>${user.user_first_name}</td>
            <td>${user.user_middle_name}</td>
        </tr>
    </c:forEach>
</table>

(note: keep the XSS attack hole in mind if you really intend to redisplay them as input values)

By the way, I'd work on your Java code conventions. Those underscores are really not Java-ish.

like image 155
BalusC Avatar answered Nov 15 '22 17:11

BalusC