HttpServletRequest类中的getAttribute()和getParameter()方法有什么区别?
当前回答
getParameter()返回http请求参数。那些从客户端传递到服务器的信息。例如http://example.com/servlet?parameter=1。只能返回字符串 getAttribute()仅供服务器端使用—您可以在同一个请求中使用属性填充请求。例如,在servlet中设置一个属性,然后从JSP中读取它。可以用于任何对象,而不仅仅是字符串。
其他回答
从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.
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()返回http请求参数。那些从客户端传递到服务器的信息。例如http://example.com/servlet?parameter=1。只能返回字符串 getAttribute()仅供服务器端使用—您可以在同一个请求中使用属性填充请求。例如,在servlet中设置一个属性,然后从JSP中读取它。可以用于任何对象,而不仅仅是字符串。
重要的是要知道属性不是参数。
属性的返回类型是Object,而参数的返回类型是String。在调用getAttribute(字符串名称)方法时,请记住必须强制转换属性。
此外,没有servlet特定的属性,也没有会话参数。
这篇文章的目的是联系@Bozho的回复,作为对其他人有用的额外信息。
getParameter -用于从客户端的HTML页面获取所需的信息
getAttribute——用于获取先前在另一个或相同的JSP或Servlet页面中设置的参数。
基本上,如果您正在从一个jsp/servlet转发或只是从一个jsp/servlet转到另一个jsp/servlet,那么除非您选择将它们放在一个对象中并使用set-属性存储在一个会话变量中,否则就无法获得所需的信息。
使用getAttribute,您可以检索Session变量。
推荐文章
- 如何排序一个数组列表在Java
- 是否有(Java)包组织的最佳实践?
- 如何从Eclipse中的开放资源对话框中隐藏.class文件?
- 将日期字符串解析为java.util.Date时,不合法的模式字符'T'
- 使用Mockito的泛型“any()”方法
- 如何使用Java属性文件?
- 我如何修复一个NoSuchMethodError?
- Maven surefire找不到ForkedBooter类
- Java 8:我如何在流中使用异常抛出方法?
- 去下一次迭代在java For循环
- 在Java中使用什么数据类型来表示钱?
- Class.getResource()和ClassLoader.getResource()之间的区别是什么?
- 如何通过传递特定日期来确定星期几?
- 如何将DecimalFormat的小数分隔符从逗号更改为点/点?
- 控制jar工件的Maven最终名称