getAttribute getParameter 区别

下面整理一下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"); %>

request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段。

getAttribute是返回对象,getParameter返回字符串 总的来说:request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。

经验分享 程序员 微信小程序 职场和发展