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

查看:40
本文介绍了如何使用 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>!");
    }
%>

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

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