HttpServletRequest.getParameter("modelName"); 能取到想要的modelObject吗?显然是不能的,原因:当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getAttribute方法来取得要共享的数据。在实际开发过程中,我们所用到的MVC中用的就是Web组件之间的转发!
下面是getParameter和getAttribute的区别和各自的使用范围。
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:
<a href="authenticate.jsp?username=wolf">authenticate.jsp </a>
或者:
<form name="form1" method="post" action="authenticate.jsp">
请输入用户姓名:
<input type="text" name="username">
<input type="submit" name="Submit" value="提交">
</form>
在authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:
<% String username=request.getParameter("username"); %>
(3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。
假定 authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字, 如何传递这一数据呢?先在authenticate.jsp中调用setAttribute()方法:
<% String username=request.getParameter("username"); request.setAttribute("username",username); %>
<jsp:forward page="hello.jsp" />
在hello.jsp中通过getAttribute()方法获得用户名字:
<% String username=(String)request.getAttribute("username"); %> Hello: <%=username %>
从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。
request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据。
request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据。
request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段。
getAttribute是返回对象,getParameter返回字符串 总的来说:request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。
再举例说明:
request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设置值。
request.getParameter():提取发送过来的参数如:http://localhost:8080/bbg.fms/file/add?fileid=12
String fileId = request.getParameter("fileid");
system.out.print(fileId);
-----------------------------------------------------------------------------------------------------------------------
request.getParameter是用来接受来自get方法或post方法的参数
<form method=post> </form>
<form method=get> </form>
<a href="hotel.jsp?hotel_id=1">ok</a>
并且request.getParameter只能接受java.lang.String,也就是说String hotel_id = request.getParameter("hotel_id");
request.getAttribute 是用来接受来自servlet的变量或Action(其实Action就是特殊的Servlet)
我们可以在Action中,request.setAttribute("ret",ret);
而且request.getAttribute只能接受java.lang.Object ,也就是说List ret = (List)request.getAttribute("ret");所以说,如果你只用JSP,根本用不到request.getAttribute()。
现在可以归纳出两者的区别了:
request.getAttribute()和request.getParameter()的核心区别是:
request.getAttribute()获得的是对象类型,而request.getParameter()获得的是字符串类型。
对于一般的网页应用,基本上是基于Post方式的传递,用getParameter取值。对于自己控制的,可以通request.setAttribute和getAttribute来实现值得传递。
经验:
当用forward标签及request.getRequestDispatcher.forward()时,可以用setAttribute和getAttribut。
表单和超链接的提交都只能读取name属性相关的value,此时用getParameter();(request.setAttribute无法用表单提交的原因:设定值的request,与表单请求的request不是同一个request,不是同一时间生成的。)
reponse.sendRediretion("hotel.jsp?hotel_id=1"),用getParameter("hotel_id")来提取.
此处再简要说明一下Get/Post:
Get 方法通过 URL 请求来传递用户的数据,将表单内各字段名称与其内容,以成对的字符串连接,置于 action 属性所指程序的 url 后,如[url]http://localhost:8080/bbg.fms/file/add?fileid=12&filename=bbg[/url],数据都会直接显示在 url 上,就像用户点击一个链接一样;
Post 方法通过 HTTP的post 机制,将表单内各字段名称与其内容放置在 HTML 表头(header)内一起传送给服务器端交由 action 属性能所指的程序处理,该程序会通过标准输入(stdin)方式,将表单的数据读出并加以处理。
参考:
http://www.cnblogs.com/jirglt/archive/2012/10/25/2738617.html
http://blog.sina.com.cn/s/blog_600046120100tnaj.html