在JSP中如何从URL获取参数?
例如,我有一个 URL www.somesite.com/Transaction_List.jsp?accountID=5
我想要5。
是否有像 session 或类似的东西那样的 request.getAttribute( "accountID") ?
最佳答案
关于Implicit Objects Unified Expression Language的,Java EE 5 Tutorial写道:
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 valueparamValues
: Maps a request parameter name to an array of valuesheader
: Maps a request header name to a single valueheaderValues
: Maps a request header name to an array of valuescookie
: Maps a cookie name to a single cookieinitParam
: 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 valuesrequestScope
: Maps request-scoped variable names to their valuessessionScope
: Maps session-scoped variable names to their valuesapplicationScope
: Maps application-scoped variable names to their values
有趣的部分以粗体显示:)
因此,要回答您的问题,您应该能够像这样访问它(使用 EL):
${param.accountID}
或者,使用 JSP Scriptlets (不推荐):
<%
String accountId = request.getParameter("accountID");
%>
关于java - 如何使用JSP从URL中获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60640618/