---
title: "jsp中getParameter与getAttribute之间的区别"
author: "1011729483"
date: "2015-09-28T14:02:22Z"
tags: []
categories: ["Web前端"]
slug: "jsp-getparameter-getattribute"
draft: false
lastmod: "2015-09-28T14:02:22Z"
id: 2246526
url: "https://ailemon.iteye.com/blog/2246526"
---

jsp中getParameter与getAttribute有何区别？


——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的值；
另外，可以用setAttribute,getAttribute发送接收对象.而getParameter显然只能传字符串。


setAttribute 是应用服务器把这个对象放在该页面所对应的一块内存中去，当你的页面服务器重定向到另一个页面时，应用服务器会把这块内存拷贝另一个页面所对应的内存中。 这样getAttribute就能取得你所设下的值，当然这种方法可以传对象。session也一样，只是对象在内存中的生命周期不一样而已。
getParameter只是应用服务器在分析你送上来的request页面的文本时，取得你设在表单或url重定向时的值。

request.getParameter 
是用来接受来自get方法或post方法的参数 
<form method=post> 
<form method=get> 
<a href="1.jsp?id=1">ok</a> 
只能接受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()

一般的网页应用，如同 chenshaizi(陈绍彬) 所说，基本上是基于Post方式的传递，用getParameter取值。对于自己控制的，可以通过request.setAttribute和getAttribute 实现值得传递。 
对于应用Structs框架的，getAttribute用的多一点，其他的基本上用getParameter


getParameter(),获取表单的值 getAttribute()获得session的值
getParameterNames() 获得表单或则url中的参数的数组
getattributeNames()：返回request对象所有属性的名字，结果集是一个Enumeration（枚举）类的实例


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