使用scriptlet访问Get参数 [英] Access Get parameter with a scriptlet

查看:149
本文介绍了使用scriptlet访问Get参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址,例如 search.do?offset=20

偏移有时会在网址中有时没有。如果它不在URL中,我希望它为0.。

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

我尝试使用scriptlet检索值,但没有成功,如下所示:

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?

推荐答案

你应该改用它。

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

小心,因为如果offset参数的整数表示不正确,将抛出NumberFormatException。

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

这是基本的JSP。您可以使用Struts或其他J2EE框架来使这些转换更安全。

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

这篇关于使用scriptlet访问Get参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆