Java 如何使用 JSP 从 URL 获取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1890438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:35:10  来源:igfitidea点击:

How to get parameters from the URL with JSP

javajspurlget-method

提问by Josh Curren

In JSP how do I get parameters from the URL?

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

For example I have a URL www.somesite.com/Transaction_List.jsp?accountID=5
I want to get the 5.

Is there a request.getAttribute( "accountID" ) like there is for sessions or something similar?

例如,我有一个www.somesite.com/Transaction_List.jsp?accountID=5
要获取 5的 URL 。

是否有 request.getAttribute("accountID") 就像会话或类似的东西?

采纳答案by Sajad Bahmani

In a GET request, the request parameters are taken from the query string (the data following the question mark on the URL). For example, the URL http://hostname.com?p1=v1&p2=v2contains two request parameters - - p1 and p2. In a POST request, the request parameters are taken from both query string and the posted data which is encoded in the body of the request.

在 GET 请求中,请求参数取自查询字符串(URL 上问号后面的数据)。例如,URL http://hostname.com?p1=v1&p2=v2包含两个请求参数——p1 和 p2。在 POST 请求中,请求参数取自查询字符串和在请求正文中编码的发布数据。

This example demonstrates how to include the value of a request parameter in the generated output:

此示例演示如何在生成的输出中包含请求参数的值:

Hello <b><%= request.getParameter("name") %></b>!

If the page was accessed with the URL:

如果页面是通过 URL 访问的:

http://hostname.com/mywebapp/mypage.jsp?name=John+Smith

the resulting output would be:

结果输出将是:

Hello <b>John Smith</b>!

If name is not specified on the query string, the output would be:

如果未在查询字符串中指定名称,则输出将为:

Hello <b>null</b>!

This example uses the value of a query parameter in a scriptlet:

此示例使用 scriptlet 中查询参数的值:

<%
    if (request.getParameter("name") == null) {
        out.println("Please enter your name.");
    } else {
        out.println("Hello <b>"+request. getParameter("name")+"</b>!");
    }
%>

回答by johannes

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

回答by johannes

request.getParameter("accountID")is what you're looking for. This is part of the Java Servlet API. See http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.htmlfor more information.

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

回答by Taylor Leese

Use EL (JSP Expression Language):

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

${param.accountID}

${param.accountID}

回答by Pascal Thivent

About the Implicit Objectsof the Unified Expression Language, the Java EE 5 Tutorialwrites:

关于隐式对象中的统一表达式语言,在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

隐式对象

JSP 表达式语言定义了一组隐式对象:

  • pageContext:JSP 页面的上下文。提供对各种对象的访问,包括:
    • servletContext:JSP 页面的 servlet 和包含在同一应用程序中的任何 Web 组件的上下文。请参阅访问 Web 上下文。
    • session: 客户端的会话对象。请参阅维护客户端状态。
    • request:触发​​JSP页面执行的请求。请参阅从请求中获取信息
    • response:JSP 页面返回的响应。请参阅构建响应。
  • 此外,还提供了几个隐式对象,可以轻松访问以下对象:
    • param: 将请求参数名称映射到单个值
    • paramValues: 将请求参数名称映射到值数组
    • header: 将请求头名称映射到单个值
    • headerValues: 将请求头名称映射到值数组
    • cookie: 将 cookie 名称映射到单个 cookie
    • initParam:将上下文初始化参数名称映射到单个值
  • 最后,有些对象允许访问使用作用域对象中描述的各种作用域变量。
    • pageScope: 将页面范围的变量名称映射到它们的值
    • requestScope: 将请求范围的变量名称映射到它们的值
    • sessionScope: 将会话范围的变量名称映射到它们的值
    • applicationScope: 将应用程序范围的变量名称映射到它们的值

The interesting parts are in bold :)

有趣的部分以粗体显示:)

So, to answer your question, you should be able to access it like this (using EL):

因此,要回答您的问题,您应该能够像这样访问它(使用 EL):

${param.accountID}

Or, using JSP Scriptlets(not recommended):

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

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

回答by Léa Massiot

If I may add a comment here...

如果我可以在这里添加评论...

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

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

doesn't work for me (it prints a 0).

对我不起作用(它打印一个 0)。

Instead, this works:

相反,这有效:

<c:out value="${param['accountID']}"></c:out>

<c:out value="${param['accountID']}"></c:out>

回答by Ainz Ooal Gown

example you wanted to delete the subject record with its subject_id

例如,您想删除带有 subject_id 的主题记录

@RequestMapping(value="subject_setup/delete/{subjectid}",method = RequestMethod.GET)
public ModelAndView delete(@PathVariable int subjectid) {
    subjectsDao.delete(subjectid);
    return new ModelAndView("redirect:/subject_setup");
}

and the parameter will be used for input on your query

并且该参数将用于您查询的输入

public int delete(int subjectid) {
    String sql = "update tbl_subject set isdeleted= '1' where id = "+subjectid+"";
    return template.update(sql);
}

回答by jay Patel

www.somesite.com/Transaction_List.jsp?accountID=5

www.somesite.com/Transaction_List.jsp?accountID=5

For this URLthere is a method call request.getParameterin java , if you want a number here cast into int, similarly for string value cast into string. so for your requirement , just copy past below line in page,

为此,java 中URL有一个方法调用request.getParameter,如果您想在此处将数字转换为int,类似于字符串值转换为string。所以根据您的要求,只需在页面中复制以下行,

int  accountId =(int)request.getParameter("accountID");

you can now call this value useing accountIdin whole page.

您现在可以accountId在整个页面中调用此值。

here accountIdis name of parameter you can also get more than one parameters using this, but this not work. It will only work with GETmethod if you hit POSTrequest then their will be an error.

accountId是参数的名称,您也可以使用它获得多个参数,但这不起作用。GET如果您点击POST请求,它只会与方法一起使用,那么它们将是一个错误。

Hope this is helpful.

希望这是有帮助的。

回答by null

page 1 : Detail page 2 : <% String id = request.getParameter("userid");%> // now you can using id for sql query of hsql detail product

page 1 : Detail page 2 : <% String id = request.getParameter("userid");%> // 现在你可以使用 id 进行 hsql 详细产品的 sql 查询