HttpServletRequest类中的getAttribute()和getParameter()方法有什么区别?
当前回答
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)。
其他回答
通常,参数是一个字符串值,最常见的是从客户端发送到服务器(例如表单post)并从servlet请求检索的值。令人沮丧的例外是ServletContext初始参数,它们是在web.xml中配置的字符串参数,存在于服务器上。
属性是存在于指定范围内的服务器变量,即:
应用程序,在整个应用程序的生命周期内可用 会话,在会话的生命周期内可用 请求,仅在请求的生命期内可用 页面(仅限JSP),仅对当前JSP页面可用
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.
getParameter()返回http请求参数。那些从客户端传递到服务器的信息。例如http://example.com/servlet?parameter=1。只能返回字符串 getAttribute()仅供服务器端使用—您可以在同一个请求中使用属性填充请求。例如,在servlet中设置一个属性,然后从JSP中读取它。可以用于任何对象,而不仅仅是字符串。
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)。
推荐文章
- 到底是什么导致了堆栈溢出错误?
- 为什么Android工作室说“等待调试器”如果我不调试?
- Java:路径vs文件
- ExecutorService,如何等待所有任务完成
- Maven依赖Servlet 3.0 API?
- 如何在IntelliJ IDEA中添加目录到应用程序运行概要文件中的类路径?
- getter和setter是糟糕的设计吗?相互矛盾的建议
- Android room persistent: AppDatabase_Impl不存在
- Java的String[]在Kotlin中等价于什么?
- Intellij IDEA上的System.out.println()快捷方式
- 使用Spring RestTemplate获取JSON对象列表
- Spring JPA选择特定的列
- URLEncoder不能翻译空格字符
- Java中的super()
- 如何转换JSON字符串映射<字符串,字符串>与杰克逊JSON