Search code examples
jspargumentsinternal-server-errorget-request

Http 500 Internal Server Error


http://localhost:8080/file.jsp?arg1=&arg2=11

runs successfully

http://localhost:8080/file.jsp?arg1=&arg2=

(empty value for argument 'arg2')

shows 500 Internal Server Error

The value of 'arg2' is used by an 'int' variable in 'jsp' using Integer.parseInt(request.getParameter("arg2")

How to fix it?


Solution

  • It is because you are trying to parse "" with Integer.parseInt

    Check Integer parseInt() API

    When you do not pass any values Like below

    http://localhost:8080/file.jsp?arg1=&arg2=
    

    Empty string will be passed in http request parameters.

    So, better put empty check when parsing integer from string.

    int arg2 = !"".equals(request.getParameter("arg2")) ? 
                    Integer.parseInt(request.getParameter("arg2")): 0;