Access Get parameter with a scriptlet

微笑、不失礼 提交于 2019-12-12 12:45:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/280714/access-get-parameter-with-a-scriptlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!