HttpServletRequest类中的getAttribute()和getParameter()方法有什么区别?
getParameter()返回http请求参数。那些从客户端传递到服务器的信息。例如http://example.com/servlet?parameter=1。只能返回字符串 getAttribute()仅供服务器端使用—您可以在同一个请求中使用属性填充请求。例如,在servlet中设置一个属性,然后从JSP中读取它。可以用于任何对象,而不仅仅是字符串。
通常,参数是一个字符串值,最常见的是从客户端发送到服务器(例如表单post)并从servlet请求检索的值。令人沮丧的例外是ServletContext初始参数,它们是在web.xml中配置的字符串参数,存在于服务器上。
属性是存在于指定范围内的服务器变量,即:
应用程序,在整个应用程序的生命周期内可用 会话,在会话的生命周期内可用 请求,仅在请求的生命期内可用 页面(仅限JSP),仅对当前JSP页面可用
重要的是要知道属性不是参数。
属性的返回类型是Object,而参数的返回类型是String。在调用getAttribute(字符串名称)方法时,请记住必须强制转换属性。
此外,没有servlet特定的属性,也没有会话参数。
这篇文章的目的是联系@Bozho的回复,作为对其他人有用的额外信息。
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 -用于从客户端的HTML页面获取所需的信息
getAttribute——用于获取先前在另一个或相同的JSP或Servlet页面中设置的参数。
基本上,如果您正在从一个jsp/servlet转发或只是从一个jsp/servlet转到另一个jsp/servlet,那么除非您选择将它们放在一个对象中并使用set-属性存储在一个会话变量中,否则就无法获得所需的信息。
使用getAttribute,您可以检索Session变量。
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(),这都是服务器端完成的。你将属性添加到请求中,并将请求提交给另一个资源,客户端不知道这一点。所以所有处理这个问题的代码通常都在servlet中。getAttribute总是返回对象。
getAttribute()和getParameter()之间的基本区别是返回类型。
java.lang.Object getAttribute(java.lang.String name)
java.lang.String getParameter(java.lang.String name)
request.getParameter ()
我们使用request. getparameter()来提取请求参数(即通过发布html表单发送的数据)。request.getParameter()总是返回String值,数据来自客户端。
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.
例如,考虑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=您,浏览器上的输出为
From Which Page : *first.jsp*
Data From Client : you
getAttribute()和getParameter()之间的基本区别是,第一个方法提取一个(序列化的)Java对象,而另一个方法提供一个String值。对于这两种情况,都会给出一个名称,以便可以查找和提取它的值(无论是字符串还是java bean)。
从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.
另一种应该使用.getParameter()的情况是在jsp中转发参数:
<jsp:forward page="destination.jsp">
<jsp:param name="userName" value="hamid"/>
</jsp:forward>
在destination.jsp中,你可以像这样访问userName:
request.getParameter("userName")
推荐文章
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?
- 在Android中将字符串转换为Uri
- 从JSON生成Java类?
- 为什么java.util.Set没有get(int index)?
- Swing和AWT的区别是什么?
- 为什么Java流是一次性的?
- 四舍五入BigDecimal *总是*有两位小数点后
- 设计模式:工厂vs工厂方法vs抽象工厂
- Java:检查enum是否包含给定的字符串?
- 它的意思是:序列化类没有声明一个静态的最终serialVersionUID字段?