getAttribute() 和 getParameter() 之间的区别

发布于 2024-10-20 18:22:48 字数 108 浏览 8 评论 0原文

HttpServletRequest 中的 getAttribute()getParameter() 方法有什么区别?

What are the differences between the getAttribute() and the getParameter() methods in HttpServletRequest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

So要识趣 2024-10-27 18:22:48
  • getParameter() 返回 http 请求参数。这些从客户端传递到服务器。例如http://example.com/servlet?parameter=1。它们只能返回String

  • getAttribute() 仅用于服务器端使用 - 您使用可以在同一请求中使用的属性填充请求。例如 - 您在 Servlet 中设置一个属性并从 JSP 中读取它。这些可以用于任何对象,而不仅仅是字符串。

  • getParameter() returns http request parameters. These are passed from the client to the server. For example http://example.com/servlet?parameter=1. They can only return String.

  • getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a Servlet and read it from a JSP. These can be used for any object, not just string.

隔纱相望 2024-10-27 18:22:48

一般来说,参数是一个字符串值,最常见的是从客户端发送到服务器(例如表单帖子)并从 servlet 请求。令人沮丧的例外是 ServletContext初始参数是在web.xml中配置并存在于服务器上的字符串参数。

属性是存在于指定范围内的服务器变量,即:

  • application,在整个应用程序的生命周期内可用
  • session,在会话的生命周期内可用
  • request,仅适用于请求的生命周期
  • page(仅限 JSP),仅适用于当前 JSP 页面

Generally, a parameter is a string value that is most commonly known for being sent from the client to the server (e.g. a form post) and retrieved from the servlet request. The frustrating exception to this is ServletContext initial parameters which are string parameters that are configured in web.xml and exist on the server.

An attribute is a server variable that exists within a specified scope i.e.:

  • application, available for the life of the entire application
  • session, available for the life of the session
  • request, only available for the life of the request
  • page (JSP only), available for the current JSP page only
只想待在家 2024-10-27 18:22:48

request.getParameter()

我们使用request.getParameter()来提取请求参数(即通过发布html表单发送的数据)。 request.getParameter() 始终返回 String 值,数据来自客户端。

request.getAttribute()

我们使用 request.getAttribute() 来获取添加到服务器端请求范围的对象,即使用 request.setAttribute()< /代码>。您可以在此处添加您喜欢的任何类型的对象,字符串、自定义对象,实际上是任何对象。您将属性添加到请求并将请求转发到另一个资源,客户端不知道这一点。因此,处理此问题的所有代码通常都在 JSP/servlet 中。您可以使用 request.setAttribute() 添加额外信息并将当前请求转发/重定向到另一个资源。

例如,考虑一下first.jsp

//First Page : first.jsp
<%@ page import="java.util.*" import="java.io.*"%>
<% request.setAttribute("PAGE", "first.jsp");%>
<jsp:forward page="/second.jsp"/>

和second.jsp:

<%@ page import="java.util.*" import="java.io.*"%>
From Which Page : <%=request.getAttribute("PAGE")%><br>
Data From Client : <%=request.getParameter("CLIENT")%>

从您的浏览器运行first.jsp?CLIENT=you,浏览器上的输出是

From Which Page : *first.jsp*
Data From Client : you

getAttribute() 之间的基本区别getParameter() 的区别是,第一个方法提取(序列化的)Java 对象,另一个提供 String 值。对于这两种情况,都会给出一个名称,以便可以查找和提取其值(无论是字符串还是 java bean)。

request.getParameter()

We use request.getParameter() to extract request parameters (i.e. data sent by posting a html form ). The request.getParameter() always returns String value and the data come from client.

request.getAttribute()

We use request.getAttribute() to get an object added to the request scope on the server side i.e. using request.setAttribute(). You can add any type of object you like here, Strings, Custom objects, in fact any object. You add the attribute to the request and forward the request to another resource, the client does not know about this. So all the code handling this would typically be in JSP/servlets. You can use request.setAttribute() to add extra-information and forward/redirect the current request to another resource.

For example,consider about first.jsp,

//First Page : first.jsp
<%@ page import="java.util.*" import="java.io.*"%>
<% request.setAttribute("PAGE", "first.jsp");%>
<jsp:forward page="/second.jsp"/>

and second.jsp:

<%@ page import="java.util.*" import="java.io.*"%>
From Which Page : <%=request.getAttribute("PAGE")%><br>
Data From Client : <%=request.getParameter("CLIENT")%>

From your browser, run first.jsp?CLIENT=you and the output on your browser is

From Which Page : *first.jsp*
Data From Client : you

The basic difference between getAttribute() and getParameter() is that the first method extracts a (serialized) Java object and the other provides a String value. For both cases a name is given so that its value (be it string or a java bean) can be looked up and extracted.

江心雾 2024-10-27 18:22:48

重要的是要知道属性不是参数

属性的返回类型是对象,而参数的返回类型是字符串。调用 getAttribute(String name) 方法时,请记住必须强制转换属性。

此外,没有 servlet 特定属性,也没有会话参数

写这篇文章的目的是为了连接 @Bozho 的回复,作为对其他人有用的附加信息。

It is crucial to know that attributes are not parameters.

The return type for attributes is an Object, whereas the return type for a parameter is a String. When calling the getAttribute(String name) method, bear in mind that the attributes must be cast.

Additionally, there is no servlet specific attributes, and there are no session parameters.

This post is written with the purpose to connect on @Bozho's response, as additional information that can be useful for other people.

不乱于心 2024-10-27 18:22:48

getAttribute 和 getParameter 之间的区别在于 getParameter 将返回由 HTML 表单提交的参数值或包含在查询字符串中的参数值。 getAttribute 返回您在请求中设置的对象,使用它的唯一方法是与 RequestDispatcher 结合使用。您使用 RequestDispatcher 将请求转发到另一个资源(JSP / Servlet)。因此,在转发请求之前,您可以设置一个可供下一个资源使用的属性。

The difference between getAttribute and getParameter is that getParameter will return the value of a parameter that was submitted by an HTML form or that was included in a query string. getAttribute returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource (JSP / Servlet). So before you forward the request you can set an attribute which will be available to the next resource.

裂开嘴轻声笑有多痛 2024-10-27 18:22:48

-getParameter() :

<html>
    <body>
        <form name="testForm" method="post" action="testJSP.jsp">
            <input type="text" name="testParam" value="ClientParam">
            <input type="submit">
        </form>
    </body>
</html>

<html>
    <body>
    <%
    String sValue = request.getParameter("testParam");
    %>
    <%= sValue %>
    </body>
</html>

request.getParameter("testParam") 将从名为“testParam”的输入框的发布表单中获取值,即“Client param”。然后它将打印出来,因此您应该在屏幕上看到“Client Param”。因此 request.getParameter() 将检索客户端提交的值。您将在服务器端获取该值。

-getAttribute() :
request.getAttribute(),这都是服务器端完成的。您将属性添加到请求中并将请求提交到另一个资源,客户端不知道这一点。因此,处理此问题的所有代码通常都在 servlets.getAttribute 中,始终返回对象。

-getParameter() :

<html>
    <body>
        <form name="testForm" method="post" action="testJSP.jsp">
            <input type="text" name="testParam" value="ClientParam">
            <input type="submit">
        </form>
    </body>
</html>

<html>
    <body>
    <%
    String sValue = request.getParameter("testParam");
    %>
    <%= sValue %>
    </body>
</html>

request.getParameter("testParam") will get the value from the posted form of the input box named "testParam" which is "Client param". It will then print it out, so you should see "Client Param" on the screen. So request.getParameter() will retrieve a value that the client has submitted. You will get the value on the server side.

-getAttribute() :
request.getAttribute(), this is all done server side. YOU add the attribute to the request and YOU submit the request to another resource, the client does not know about this. So all the code handling this would typically be in servlets.getAttribute always return object.

淤浪 2024-10-27 18:22:48

getParameter - 用于从客户端的 HTML 页面获取所需的信息

getAttribute - 用于获取先前在另一个或同一 JSP 或 Servlet 页面中设置的参数。

基本上,如果您要转发或只是从一个 jsp/servlet 转到另一个 jsp/servlet,则无法获得所需的信息,除非您选择将它们放入对象中并使用设置属性存储在会话变量中。

使用 getAttribute,您可以检索 Session 变量。

getParameter - Is used for getting the information you need from the Client's HTML page

getAttribute - This is used for getting the parameters set previously in another or the same JSP or Servlet page.

Basically, if you are forwarding or just going from one jsp/servlet to another one, there is no way to have the information you want unless you choose to put them in an Object and use the set-attribute to store in a Session variable.

Using getAttribute, you can retrieve the Session variable.

粉红×色少女 2024-10-27 18:22:48

来自 http://www.coderanch.com/t/ 361868/Servlets/java/request-getParameter-request-getAttribute

“参数”是从客户端发送到服务器的名称/值对
- 通常,来自 HTML 表单。参数只能有字符串值。有时(例如使用 GET 请求)您会看到这些
直接编码到 URL 中(在 ? 之后,每个都采用以下形式
名称=值,每对由 & 分隔)。其他时候,他们是
使用 POST 等方法时,包含在请求正文中。

“属性”是一种服务器本地存储机制 - 不存储任何内容
作用域属性永远会在服务器外部传输,除非您
明确地做到这一点。属性具有字符串名称,但存储
对象值。请注意,属性是 Java 特有的(它们存储
Java 对象),而参数是与平台无关的(它们是
仅由通用字节组成的格式化字符串)。

属性总共有四个范围:“page”(用于 JSP 和标签)
仅文件),“请求”(仅限于当前客户端的请求,
请求完成后销毁),“会话”(存储在
客户端的会话,会话终止后失效),
“应用程序”(存在以供所有组件在整个过程中访问)
应用程序的部署生命周期)。

底线是:从获取数据时使用参数
客户端,在服务器上存储对象时使用作用域属性
仅由您的应用程序内部使用。

from http://www.coderanch.com/t/361868/Servlets/java/request-getParameter-request-getAttribute

A "parameter" is a name/value pair sent from the client to the server
- typically, from an HTML form. Parameters can only have String values. Sometimes (e.g. using a GET request) you will see these
encoded directly into the URL (after the ?, each in the form
name=value, and each pair separated by an &). Other times, they are
included in the body of the request, when using methods such as POST.

An "attribute" is a server-local storage mechanism - nothing stored in
scoped attribues is ever transmitted outside the server unless you
explicitly make that happen. Attributes have String names, but store
Object values. Note that attributes are specific to Java (they store
Java Objects), while parameters are platform-independent (they are
only formatted strings composed of generic bytes).

There are four scopes of attributes in total: "page" (for JSPs and tag
files only), "request" (limited to the current client's request,
destroyed after request is completed), "session" (stored in the
client's session, invalidated after the session is terminated),
"application" (exist for all components to access during the entire
deployed lifetime of your application).

The bottom line is: use parameters when obtaining data from the
client, use scoped attributes when storing objects on the server for
use internally by your application only.

寄居者 2024-10-27 18:22:48

另一种应该使用 .getParameter() 的情况是在 jsp 中使用参数进行转发时:

<jsp:forward page="destination.jsp">
    <jsp:param name="userName" value="hamid"/>
</jsp:forward>

destination.jsp 中,您可以像这样访问 userName :

request.getParameter("userName")

Another case when you should use .getParameter() is when forwarding with parameters in jsp:

<jsp:forward page="destination.jsp">
    <jsp:param name="userName" value="hamid"/>
</jsp:forward>

In destination.jsp, you can access userName like this:

request.getParameter("userName")
橘虞初梦 2024-10-27 18:22:48

getAttribute()getParameter() 之间的基本区别在于返回类型。

java.lang.Object getAttribute(java.lang.String name)
java.lang.String getParameter(java.lang.String name)

Basic difference between getAttribute() and getParameter() is the return type.

java.lang.Object getAttribute(java.lang.String name)
java.lang.String getParameter(java.lang.String name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文