error - java.lang.NumberFormatException: para la cadena de entrada en la página JSP
for input string java (1)
Los ${userRecords}
aquí
${userRecords.user_first_name}
${userRecords.user_middle_name}
es una List<User>
, sin embargo, está intentando acceder a ella como si fuera un User
único. Esto no es válido En EL, solo se puede acceder a una List
con un índice entero, que indique la posición del elemento de la lista al que desea acceder, como
${userRecords[0].user_first_name}
${userRecords[0].user_middle_name}
La excepción también dice básicamente que esperaba un valor entero en lugar de un valor de cadena. Si miras más de cerca el rastro de la pila, verás que está involucrado un ListELResolver
.
Sin embargo, tu problema concreto es más grande. En realidad, deberías iterar sobre la lista. Use el JSTL <c:forEach>
para eso. Por ejemplo (simplificado a partir de su fragmento de código impar):
<table>
<c:forEach items="${userRecords}" var="user">
<tr>
<td>${user.user_first_name}</td>
<td>${user.user_middle_name}</td>
</tr>
</c:forEach>
</table>
(nota: tenga en cuenta el agujero de ataque XSS si realmente desea volver a mostrarlos como valores de entrada)
Por cierto, trabajaría en las convenciones de tu código Java . Esos guiones bajos realmente no son Java-ish.
Hola, ¿alguien puede ayudarme por qué esto está dando un error al tratar de mostrar los valores en una página JSP? No tengo ningún número convertido o String se ha convertido en Número, sin embargo recibo NumberFormatException
mi Servlet recibe la solicitud para mostrar un registro de usuario
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;
}
Página 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>
.......
Excepción que recibo:
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)