java - 如何使用JSP从URL中获取参数

标签 java jsp url get-method

在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 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 Scriptlets (不推荐):

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

关于java - 如何使用JSP从URL中获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60640618/

相关文章:

Java 在单词中间用引号分隔字符串

java - 警告 : Could not find action or result - Struts2

java - 迭代从 JSP 上的 servlet 发送的 ArrayList?

java - 在 Java 中从 URL 中读取 XML 字符串

javascript - 在父窗口中打开 URL

c# - 如何从 C# App_Code 类中获取当前页面的 URL?

java.home 属性在写入属性文件时会添加额外的斜杠吗?

java - 将 SwingWorker 的通用类型设置为 void?

java - layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) 方法中的 offsets.length 或 spans.length 是什么?

java - 使用 json 数据创建动态 TreeView - jsp 中的 dynatree