如何使用 JSP 从 URL 获取参数
在 JSP 中如何从 URL 获取参数?
In JSP how do I get parameters from the URL?
例如我有一个 URL www.somesite.com/Transaction_List.jsp?accountID=5
我想要5.
是否有 request.getAttribute( "accountID" ) 类似于会话或类似的东西?
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?
在 GET 请求中,请求参数取自查询字符串(URL 上问号后面的数据).例如,网址 http://hostname.com?p1=v1&p2=v2 包含两个请求参数——p1 和 p2.在 POST 请求中,请求参数取自查询字符串和在请求正文中编码的发布数据.
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=v2 contains 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.
此示例演示如何在生成的输出中包含请求参数的值:
This example demonstrates how to include the value of a request parameter in the generated output:
Hello <b><%= request.getParameter("name") %></b>!
如果页面是通过 URL 访问的:
If the page was accessed with the URL:
http://hostname.com/mywebapp/mypage.jsp?name=John+Smith
结果输出为:
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:
<%
if (request.getParameter("name") == null) {
out.println("Please enter your name.");
} else {
out.println("Hello <b>"+request. getParameter("name")+"</b>!");
}
%>