HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别:
(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法
(2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数,例如假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:
<a href="authenticate.jsp?username=weiqin">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() 和request.getAttribute() 区别
本篇文章小编为大家介绍,基于request.getAttribute与request.getParameter的区别详解。需要的朋友参考下
Integer count2 = (Integer) context.getAttribute("count"); //2.人数+1 if(count2 == null){ //第一个用户 count2 = 1; }else{ count2++; } //3.再存放到application作用域中 context.setAttribute(...
javascript request.setAttribute()详解 request.setAttribute()怎么用的? JSP1代码 String [] test=new ...String test[]=(String[])request.getAttribute(test); out.print(test); 为什么JS P2中会找不到test?
在后台通过Request取值为null,是因为只设置了id属性,而取值候用的是name属性,问题就出现在这里
request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。即request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取...
Map, Book> books = (Map, Book>)request.getSession().getServletContext().getAttribute("books"); Book book = books.get(bookid); System.out.println(book); //得到数量 int bookNum = Integer....
if (session.getAttribute("person") == null) { if (null != request.getQueryString()){ // session.setAttribute("redirectUrl", request.getRequestURL().append("?").append(request.getQueryString())....
j2ee复习资料 一、选择题 1.page 指令的(b )属性用于引用需要的包或类 ...3.使用 response 对象进行重定向时,使用的是(c )方法。 A.getAttribute B.setContentType C.sendRedirect D.setAttribute
java类中获得验证码的方法: session.getAttribute("checkcode"); jsp页面中显示图片的方式: ${pageContext.request.contextPath}/checkcode.jsp"/> 另外在web.xml中配置验证码类的加载: <servlet-name>...
session.getAttribute在servlet中是一个很重要的方法。
HttpSession session = request.getSession(); session.setAttribute("name", "zhangsan"); session.setAttribute("pwd", "aaa"); String name = (String) session.getAttribute("name"); 2.cookie: //创建Cookie ...
getAttribute()方法 至此,我们已经向大家介绍了两种检索特定元素节点...不过,getAttribute()方法不能通过document对象调用,这与我们此前介绍过的其他方法不同。我们只能通过一个元素节点对象调用它。 例如,你可以把
那么在 MyServlet 中可以使用( )方法把属性 name 的值取出来。 A. String str=request.getAttribute("name"); B. String str=(String)request.getAttribute("name"); C. String str=(String)session.getAttribute...
UserAccessToken token = (UserAccessToken) request.getSession().getAttribute("UserAccessToken"); if(null==token){ token = util.getAccessToken3(Constants.APPID, Constants.SECRET,code); ...
if(request.getAttribute("err")!=null) { %> <h2><%=request.getAttribute("err")%> } %> <form method="post" action="login-conf.jsp"> 用户名:<input type="text" name="name"><br> 密码: ...
public class Addcontentservlet extends HttpServlet { public Addcontentservlet(){ super(); } /** * Destruction of the servlet.... */ public void destroy() { ...使用数据库与用户结合
js中的getAttribute方法使用示例.docx
里面有一个jsp文件例子 ... var allEquString = '<%=request.getAttribute("allEquString")%>'; [removed] [removed][removed] </head> <body> </body> </html>
//不管在什么情况下·有没有中文的接收我们必须设置他的request的setCharacterEncoding方法 ReplyDao replyDao =new ReplyDaoImpl(); int uId=Integer.parseInt(request.getParameter("uId")); int boardId=...