ServletRequest中 getParameter 与 getAttribute的区别

getParameter 与 getAttribute的区别

request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设置值,   
request.getParameter():提取发送过来的参数如:本网页http://community.csdn.net/Expert/topic/4633/4633804.xml?temp=.3488123  
request.getParameter("temp")==".3488123"

request.getParameter
是用来接受来自get方法或post方法的参数


ok
只能接受java.lang.String
也就是说String hotel_id = request.getParameter(“hotel_id”);
request.getAttribute
是用来接受来自servlet的变量或Action(其实Action就是特殊的Servlet)
在Action中,request.setAttribute(“ret”,ret);
只能接受java.lang.Object
也就是说List ret = (List)request.getAttribute(“ret”);
如果你只用JSP,根本用不到request.getAttribute()

request.getAttribute()和request.getParameter()的区别是request.getAttribute()获得的是对象类型,

而request.getParameter()获得的是字符串类型
一般的网页应用,如同 chenshaizi(陈绍彬) 所说,基本上是基于Post方式的传递,用getParameter

取值。对于自己控制的,可以通过request.setAttribute和getAttribute 实现值得传递。
对于应用Structs框架的,getAttribute用的多一点,其他的基本上用getParameter

我的理解:
session.getAttribute();获得session
request.getParameter();获得parameter

1.getParameter可以获得客户端传送给服务器端的参数值。
getAttribute可以得到由setAttribute设置的参数值,就相当于是使用getAttribute得到一个自己定义

的参数,而不是从客户端得到的参数。
2.getParameter只能传递string类型的变量,getAttribute能传递vector。

getParameter(),获取表单的值 getAttribute()获得session的值

getParameterNames() 获得表单或则url中的参数的数组
getattributeNames():返回request对象所有属性的名字,结果集是一个Enumeration(枚举)类的实例

根据楼上,是不是 getParameter()得到的值如果下次不提交或保存起来的话,下次重定向后就没啦?
:理解对了
getAttribute()所要得到的属性值因为存在session中,所以重定向后仍可以取出?
getAttribute()在request和session中都有,只是作用域不同,在取之前肯定是要在某个地方存一下,这种

东东可以存取对象

http://community.csdn.net/Expert/topic/4763/4763471.xml?temp=.1793177
看见后面的?temp=.1793177没有,?号后面的这个东西用request.getAttribute(“temp”)就能得到字符

串”.1793177”,
而getAttribute()之前,你必须在页面或者逻辑中用serAttribute()设置了才能用,已经很清楚了,我不再

说了哈

Parameter是html裡傳來的像 checkbox textfield password radio …的value
getAttribute是取得jsp中用setAttribute設定的attribute

還有….
parameter得到的是string
attribute得到的是object


  • 个人经验:当用forward标签及request.getRequestDispatcher.forward()时,可以用setAttributegetAttribute,表单提交和超链接提交都只能读取name属性相关的value,用getParameter();reponse.sendRediretion(“MyJsp.jsp?username=luocong”),这样也可以用getParameter(“username”)来提取.

  • Get 方法通过 URL 请求来传递用户的数据,将表单内各字段名称与其内容,以成对的字符串连接,置于 action 属性所指程序的 url 后,如[url]http://www.mdm.com/test.asp?name=asd&password=sad[/url],数据都会直接显示在 url 上,就像用户点击一个链接一样;Post 方法通过 HTTP post 机制,将表单内各字段名称与其内容放置在 HTML 表头(header)内一起传送给服务器端交由 action 属性能所指的程序处理,该程序会通过标准输入(stdin)方式,将表单的数据读出并加以处理

  • request.setAttribute无法用表单提交的原因,设定值的request,与表单请求的request不是一个request,不是同一时间生成的。

来自 : http://blog.sina.com.cn/s/blog_600046120100tnaj.html


参考:

package javax.servlet;

/**
 * Defines an object to provide client request information to a servlet.  The
 * servlet container creates a <code>ServletRequest</code> object and passes
 * it as an argument to the servlet's <code>service</code> method.
 *
 * <p>A <code>ServletRequest</code> object provides data including
 * parameter name and values, attributes, and an input stream.
 * Interfaces that extend <code>ServletRequest</code> can provide
 * additional protocol-specific data (for example, HTTP data is
 * provided by {@link javax.servlet.http.HttpServletRequest}.
 * 
 * @author Various
 *
 * @see javax.servlet.http.HttpServletRequest
 *
 */
public interface ServletRequest {

    /**
     * Returns the value of the named attribute as an <code>Object</code>,
     * or <code>null</code> if no attribute of the given name exists. 
     *
     * <p> Attributes can be set two ways.  The servlet container may set
     * attributes to make available custom information about a request.
     * For example, for requests made using HTTPS, the attribute
     * <code>javax.servlet.request.X509Certificate</code> can be used to
     * retrieve information on the certificate of the client.  Attributes
     * can also be set programatically using 
     * {@link ServletRequest#setAttribute}.  This allows information to be
     * embedded into a request before a {@link RequestDispatcher} call.
     *
     * <p>Attribute names should follow the same conventions as package
     * names. This specification reserves names matching <code>java.*</code>,
     * <code>javax.*</code>, and <code>sun.*</code>. 
     *
     * @param name a <code>String</code> specifying the name of the attribute
     *
     * @return an <code>Object</code> containing the value of the attribute,
     * or <code>null</code> if the attribute does not exist
     */
    public Object getAttribute(String name);







    /**
     * Stores an attribute in this request.
     * Attributes are reset between requests.  This method is most
     * often used in conjunction with {@link RequestDispatcher}.
     *
     * <p>Attribute names should follow the same conventions as
     * package names. Names beginning with <code>java.*</code>,
     * <code>javax.*</code>, and <code>com.sun.*</code>, are
     * reserved for use by Sun Microsystems.
     *<br> If the object passed in is null, the effect is the same as
     * calling {@link #removeAttribute}.
     * <br> It is warned that when the request is dispatched from the
     * servlet resides in a different web application by
     * <code>RequestDispatcher</code>, the object set by this method
     * may not be correctly retrieved in the caller servlet.
     *
     * @param name a <code>String</code> specifying 
     * the name of the attribute
     *
     * @param o the <code>Object</code> to be stored
     *
     */
    public void setAttribute(String name, Object o);



    /**
     * Returns the value of a request parameter as a <code>String</code>,
     * or <code>null</code> if the parameter does not exist. Request parameters
     * are extra information sent with the request.  For HTTP servlets,
     * parameters are contained in the query string or posted form data.
     *
     * <p>You should only use this method when you are sure the
     * parameter has only one value. If the parameter might have
     * more than one value, use {@link #getParameterValues}.
     *
     * <p>If you use this method with a multivalued
     * parameter, the value returned is equal to the first value
     * in the array returned by <code>getParameterValues</code>.
     *
     * <p>If the parameter data was sent in the request body, such as occurs
     * with an HTTP POST request, then reading the body directly via {@link
     * #getInputStream} or {@link #getReader} can interfere
     * with the execution of this method.
     *
     * @param name a <code>String</code> specifying the name of the parameter
     *
     * @return a <code>String</code> representing the single value of
     * the parameter
     *
     * @see #getParameterValues
     */
    public String getParameter(String name);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值