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

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


当前回答

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

其他回答

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

下面的例子演示了如何在生成的输出中包含一个请求参数的值:

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

如果使用URL访问页面:

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

结果输出将是:

Hello <b>John Smith</b>!

如果查询字符串上没有指定name,输出将是:

Hello <b>null</b>!

下面的例子使用了scriptlet中查询参数的值:

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

第一页: 细节 第二页: <% String id = request.getParameter("userid") //现在您可以使用id查询HSQL详细产品

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

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

$ {param.accountID}