Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Get parameter with a scriptlet

I hava a url such as search.do?offset=20

offset sometimes is in the url sometimes not. When it is not in the URL i want it to be 0.

i try, without success, to retrieve the value with a scriptlet as follows:

<%  Integer offset = (pageContext.findAttribute("offset")==null) ? new Integer("0") : new Integer((String) pageContext.findAttribute("offset")); %>

Anyone knows what i am doing wrong?

like image 539
Sergio del Amo Avatar asked Sep 15 '26 15:09

Sergio del Amo


1 Answers

You should use this instead.

<% Integer offset = request.getParameter("offset") != null && request.getParameter("offset").length() > 0 ? new Integer(request.getParameter("offset")) : new Integer(0); %>

Be careful because if "offset" parameter has an incorrect integer representation a NumberFormatException will be thrown.

This is basic JSP. You could use Struts or other J2EE framework that make these conversions safer for you.

like image 132
Fernando Miguélez Avatar answered Sep 20 '26 02:09

Fernando Miguélez