Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  java  >  正文 java中getAttribute和getParameter的区别

java中getAttribute和getParameter的区别

发布时间:2016-12-03   编辑:www.jquerycn.cn
jquery中文网为您提供java中getAttribute和getParameter的区别等资源,欢迎您收藏本站,我们将为您提供最新的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")

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5164')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5164>

<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


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

方案一
这么改写:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7982')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7982><%=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就结束了。

您可能感兴趣的文章:
java中getAttribute和getParameter的区别
JSP 客户端请求
Java 实例
java和python的区别
php和java的区别有哪些 ?
.net、php、java的区别是什么?
Servlet 客户端 HTTP 请求
servlet与jsp基础教程(4)-处理表单数据
java和php区别大吗?
python与java用途区别有哪些

[关闭]