getAttribute() 和 getParameter() 之间的区别
HttpServletRequest
中的 getAttribute()
和 getParameter()
方法有什么区别?
What are the differences between the getAttribute()
and the getParameter()
methods in HttpServletRequest
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
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 examplehttp://example.com/servlet?parameter=1
. They can only returnString
.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 aServlet
and read it from a JSP. These can be used for any object, not just string.一般来说,参数是一个字符串值,最常见的是从客户端发送到服务器(例如表单帖子)并从 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 applicationsession
, available for the life of the sessionrequest
, only available for the life of the requestpage
(JSP only), available for the current JSP page onlyrequest.getParameter()
我们使用
request.getParameter()
来提取请求参数(即通过发布html表单发送的数据)。request.getParameter()
始终返回String
值,数据来自客户端。request.getAttribute()
我们使用
request.getAttribute()
来获取添加到服务器端请求范围的对象,即使用request.setAttribute()< /代码>。您可以在此处添加您喜欢的任何类型的对象,
字符串
、自定义对象,实际上是任何对象。您将属性添加到请求并将请求转发到另一个资源,客户端不知道这一点。因此,处理此问题的所有代码通常都在 JSP/servlet 中。您可以使用 request.setAttribute() 添加额外信息并将当前请求转发/重定向到另一个资源。例如,考虑一下first.jsp
和second.jsp:
从您的浏览器运行first.jsp?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 ). Therequest.getParameter()
always returnsString
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. usingrequest.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 userequest.setAttribute()
to add extra-information and forward/redirect the current request to another resource.For example,consider about first.jsp,
and second.jsp:
From your browser, run first.jsp?CLIENT=you and the output on your browser is
The basic difference between
getAttribute()
andgetParameter()
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.重要的是要知道属性不是参数。
属性的返回类型是对象,而参数的返回类型是字符串。调用
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.
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.
-getParameter() :
request.getParameter("testParam")
将从名为“testParam”的输入框的发布表单中获取值,即“Client param”。然后它将打印出来,因此您应该在屏幕上看到“Client Param”。因此 request.getParameter() 将检索客户端提交的值。您将在服务器端获取该值。-getAttribute() :
request.getAttribute()
,这都是服务器端完成的。您将属性添加到请求中并将请求提交到另一个资源,客户端不知道这一点。因此,处理此问题的所有代码通常都在 servlets.getAttribute 中,始终返回对象。-getParameter() :
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.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 pagegetAttribute
- 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.
来自 http://www.coderanch.com/t/ 361868/Servlets/java/request-getParameter-request-getAttribute
from http://www.coderanch.com/t/361868/Servlets/java/request-getParameter-request-getAttribute
另一种应该使用
.getParameter()
的情况是在 jsp 中使用参数进行转发时:在
destination.jsp
中,您可以像这样访问userName
:Another case when you should use
.getParameter()
is when forwarding with parameters in jsp:In
destination.jsp
, you can accessuserName
like this:getAttribute() 和 getParameter() 之间的基本区别在于返回类型。
Basic difference between getAttribute() and getParameter() is the return type.