在JSP我如何从URL获取参数?

例如,我有一个URL www.somesite.com/Transaction_List.jsp?accountID=5 我要得到5。 有什么要求吗?getAttribute("accountID")就像有会话或类似的东西吗?


当前回答

String accountID = request.getParameter("accountID");

其他回答

使用EL (JSP表达式语言):

$ {param.accountID}

request.getParameter("accountID")就是你要找的。这是Java Servlet API的一部分。更多信息请参见http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html。

如果我可以在这里补充评论的话……

< c: value = " $ {param.accountID} " > < / c: >

对我不起作用(它输出0)。

相反,这是可行的:

< c: value = " ${参数[' accountID ']} " > < / c: >

关于统一表达式语言的隐式对象,Java EE 5教程写道:

Implicit Objects The JSP expression language defines a set of implicit objects: pageContext: The context for the JSP page. Provides access to various objects including: servletContext: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context. session: The session object for the client. See Maintaining Client State. request: The request triggering the execution of the JSP page. See Getting Information from Requests. response: The response returned by the JSP page. See Constructing Responses. In addition, several implicit objects are available that allow easy access to the following objects: param: Maps a request parameter name to a single value paramValues: Maps a request parameter name to an array of values header: Maps a request header name to a single value headerValues: Maps a request header name to an array of values cookie: Maps a cookie name to a single cookie initParam: Maps a context initialization parameter name to a single value Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects. pageScope: Maps page-scoped variable names to their values requestScope: Maps request-scoped variable names to their values sessionScope: Maps session-scoped variable names to their values applicationScope: Maps application-scoped variable names to their values

有趣的部分是粗体:)

所以,为了回答你的问题,你应该能够像这样访问它(使用EL):

${param.accountID}

或者,使用JSP scriptlet(不推荐):

<%
    String accountId = request.getParameter("accountID");
%>
String accountID = request.getParameter("accountID");