如何使用JSP从URL获取参数 [英] How to get parameters from the URL with JSP

查看:142
本文介绍了如何使用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上的问号)。例如,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>!

如果使用网址访问该页面:

If the page was accessed with the URL:

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

结果输出为:

Hello <b>John Smith</b>!

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

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

Hello <b>null</b>!

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

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>!");
    }
%>

这篇关于如何使用JSP从URL获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆