Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to process numeric primary key from jsp view into Controller Servlet

 
Jay Tai
Ranch Hand
Posts: 221
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm testing a small application which basically shows a list of users (ShowMembers.jsp). When clicking on the UPDATE link, the selected user then (based on the primary key) is shown an update form where the values can be modified. The problem is that I get "HTTP Status 500 - For input string: "" error when selecting the UPDATE link from ShowMembers.jsp. I don't understand how to retrieve a numeric key using String MemIDString = 'request.getParameters("MemID") for example. I don't seem to be getting any value using that method. I also used parseInt.

I feel like I'm missing something really obvious. My code is as follows. Thanks in advance:




 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value in your HTML page for MemID in the link URL? Do a View Source at the browser to see how the URL is being formatted.
 
Jay Tai
Ranch Hand
Posts: 221
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value (source) of the jsp is as follows. The source of the resulting action is an error page. Do you want me to post that as well? What I notice is that all MemID values are being correctly picked up in the member list. The error is thrown when selecting a specific member from the list. I JUST REALIZED THAT I DIDN'T POST THE STACK TRACE WHEN I CREATED THIS THREAD. SORRY AND THE TRACE IS:


java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at admin.membs.ShowMember.doGet(ShowMember.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)







 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's your problem:

So the problem is twofold:
  • Your JSP is not filling in the URL correctly.
  • Your servlet is not checking for whether the value is missing or not and doing something better than just throwing an exception. (Or you could establish an error handler in the deployment descriptor if this is a condition that is unexpected and should never happen.)
  •  
    Jay Tai
    Ranch Hand
    Posts: 221
    Java MySQL Database Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you! I changed the last entry in the jsp from:



    to:




    I added to the servlet to see if the MemID String parameter is being picked and it is. It shows the correct number

    I then added and that too shows the same number as the above

    So it looks like MemID is being passed from the Jsp to the servet, and this gives me a blank Update page without the user values and the following error:


    java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3813)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3795)
    at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4616)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:135)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:135)
    at admin.data.MemDB.selectMember(MemDB.java:157)
    at admin.membs.ShowMember.doGet(ShowMember.java:46)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

    Does this suggest that the servlet is only getting one parameter (MemID) and doesn't pass the other values (firstname, lastname, etc) to the update page?

    The update.jsp is:

     
    Jay Tai
    Ranch Hand
    Posts: 221
    Java MySQL Database Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another odd problem is when I set the update link in the user list jap to 'admin.business.Member.memID' it throws a 'java.lang.NumberFormatException: For input string: "" error

    But when I just use member.memID it shows a blank update page without the user details, but gives a 'java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1)'

    Does this suggest that using 'admin.business.Member.memID' is the correct way to pass the MemID but the MemID is not being correctly parsed?
     
    Jay Tai
    Ranch Hand
    Posts: 221
    Java MySQL Database Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well I solved this. It was actually an incorrect method that I put in the DAO. I can post the fix if it helps although it's not related to the original post!
     
    margaret gillon
    Ranch Hand
    Posts: 335
    6
    Linux Tomcat Server Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jay. I would be interested in seeing your solution.
     
    Jay Tai
    Ranch Hand
    Posts: 221
    Java MySQL Database Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Margaret,

    I made quite a careless mistake thinking the primary key would be automatically set by the controller servlet, so I omitted the PK in the DAO CLASS. The solution was to add 'ps.setInt(11,member.getMemID());' to the following DAO method (I commented the part that I added). Please let me know if you need me to provide anymore detail. Hope this helps!


     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic