Question

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?

Était-ce utile?

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

Autres conseils

It is always better to check the query parameters against what is needed. In this case you can use org.apache.commons.lang.math.NumberUtils library to check if the entered value is a number or not and use default value in case it is not a number.

int arg2 = NumberUtils.isNumber(request.getParameter("arg2")) ? Integer.parseInt(request.getParameter("arg2")) : 0;

If you are using Internet Explorer, go to Tools - Internet Options - Advanced - under Browsing untick "Show Friendly HTTP Error Messages". This will allow you to get a better description of what the error is.

To help you with your code itself, you will need to post it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top