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


当前回答

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.

其他回答

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

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

应用程序,在整个应用程序的生命周期内可用 会话,在会话的生命周期内可用 请求,仅在请求的生命期内可用 页面(仅限JSP),仅对当前JSP页面可用

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

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

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

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

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

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

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.

从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.