java中getAttribute和getParameter的区别

 
request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据;request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。即request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。
 
 
 1.getAttribute是取得jsp中用setAttribute?定的attribute

2.parameter得到的是string;attribute得到的是object

3.request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据;request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。即request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。


4.HttpServletRequest类有setAttribute()方法,而没有setParameter()方法


5.当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,


6.当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。

——getParameter得到的都是String类型的。或者是http://a.jsp?id=123中的123,或者是某个表单提交过去的数据。
——getAttribute则可以是对象。
——getParameter()是获取POST/GET传递的参数值;
——getAttribute()是获取对象容器中的数据值;
——getParameter:用于客户端重定向时,即点击了链接或提交按扭时传值用,即用于在用表单或url重定向传值时接收数据用。
——getAttribute:用于服务器端重定向时,即在sevlet中使用了forward函数,或struts中使用了mapping.findForward。getAttribute只能收到程序用setAttribute传过来的值。
——getParameter()是获取POST/GET传递的参数值;
——getAttribute()是获取SESSION的值;


一般通过表单和链接传递的参数使用getParameter

通过request.setAttribute("name","jerry")的方式赋值的使用request.getAttribute("name")

 代码如下复制代码

<jsp:include page="login.jsp" flush="true">
        <jsp:param name="username" value="<%=Request.getParameter("username")%>" />
        <jsp:param name="password" value="<%=Request.getParameter("password")%>" />
</jsp:include>
 

具体原由有可能是tomcat的版本不同导致的。jsp在编译jsp页面时,将 <%=request.getParameter("username")%>作为变量进行处理。因此,遇到里面的"会报错。”


出现错误提示:

org.apache.jasper.JasperException: /index1.jsp (line: 14, column: 38) Attribute value Request.getParameter("username") is quoted with " which must be escaped when used within the value


解决以上问题有两种解决方案:

方案一
这么改写:

 代码如下复制代码
<%=request.getParameter("username")%>

方案二
归根结底是tomcat版本的问题,更换tomcat版本吧。

个人见解:

当用户把值作为链接参数传递给下个页面或serve时,用getParameter()获取.如aa.jsp?id=1;还有表单的提交.

当用户把值放在了request中的一个属性时(request.setAttribute("aa","tt"),"aa"这个属性名可以任意取的),用getAttribute(属性名)获取.


request范围较小一些,只是一个请求,简单说就是你在页面上的一个操作, request.getParameter()就是从上一个页面中的url、form中获取参数。但如果一个request涉及多个类,后面还要取参数,可以用request.setAttribute()和request.getAttribute(),但是当结果输出之后,request就结束了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值