Solved

JSP - How to clear the HttpServletRequest

Posted on 2007-03-25
10
3,413 Views
Last Modified: 2010-05-18
I wish to do is clear up all the parameters and attributes in the request(HttpServletRequest) pass by client, and set a new attribute, and dispatch it to another page, to summarize it in code:

//clear up all the parameters and attributes in the request pass by client<--How to do this
request.setAttribute("NewAttribute", "value");
dispatcher.forward(request, response);

I can't find a method like request.clear(). Is there a corresponding method that can do this?
0
Comment
Question by:william007
10 Comments
 
LVL 14

Assisted Solution

by:boonleng
boonleng earned 150 total points
ID: 18788074
To remove the value in attribute, call request.getAttributeNames() to get all all the attribute names, loop through the names and call request.removeAttribute() to remove it 1 by 1.

parameters value in the request cannot be removed.
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 150 total points
ID: 18788989
If you want to forward to a page without having the request, just use response.sendRedirect () instead of RequestDispatcher.forward ()
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 150 total points
ID: 18788994
Otherwise you can set the attributes to null but I doubt if you cannot change the values of parameters.
0
 
LVL 27

Expert Comment

by:rrz
ID: 18789417
This works for me in Tomcat 6.  Please try in your server.
<%@ page import="java.util.*"%>
<%!
  public class Wrapper extends HttpServletRequestWrapper{
     public Wrapper(HttpServletRequest request){
                     super(request);    
     }
     public String getParameter(java.lang.String name){return null;}
     public Map getParameterMap(){return null;}
     public Enumeration getParameterNames(){return null;}
     public String[] getParameterValues(java.lang.String name){return null;}
  }
%>
<%
  Enumeration e = request.getAttributeNames();
  while(e.hasMoreElements()){
     String attName = (String)e.nextElement();
     if(!"javax.servlet.forward.request_uri".equals(attName)
          && !"javax.servlet.forward.context_path".equals(attName)
          && !"javax.servlet.forward.servlet_path".equals(attName)
          && !"javax.servlet.forward.query_string".equals(attName))request.removeAttribute(attName);
  }
   Wrapper wrap = new Wrapper(request);
   wrap.setAttribute("NewAttribute", "someValue");
   RequestDispatcher dispatcher = application.getRequestDispatcher("/destination.jsp");
   dispatcher.forward(wrap, response);
%>
0
 
LVL 27

Expert Comment

by:rrz
ID: 18789908
On second thought, please try this one instead.
<%@ page import="java.util.*"%>
<%!
  public class Wrapper extends HttpServletRequestWrapper{
     public Wrapper(HttpServletRequest request){
                     super(request);    
     }
     public String getParameter(java.lang.String name){return null;}
     public Map getParameterMap(){return null;}
     public Enumeration getParameterNames(){return null;}
     public String[] getParameterValues(java.lang.String name){return null;}
  }
%>
<%
  Enumeration e = request.getAttributeNames();
  while(e.hasMoreElements()){
     String attName = (String)e.nextElement();
     request.removeAttribute(attName);
  }
   Wrapper wrap = new Wrapper(request);
   wrap.setAttribute("NewAttribute", "someValue");
   RequestDispatcher dispatcher = application.getRequestDispatcher("/destination.jsp");
   dispatcher.forward(wrap, response);
%>
hello from requestWrap.jsp<br/>
name is ${param.name}<br/>
location is ${location}<br/>
NewAttribute is ${NewAttribute}<br/>
<%
  Enumeration e2 = request.getAttributeNames();
  while(e2.hasMoreElements()){
     String att = (String)e2.nextElement();
     out.print(att + "=" + request.getAttribute(att) + "<br/>");
  }
%>
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Accepted Solution

by:
rrz earned 200 total points
ID: 18789916
I am sorry. Not the that last one. Try this one.
<%@ page import="java.util.*"%>
<%!
  public class Wrapper extends HttpServletRequestWrapper{
     public Wrapper(HttpServletRequest request){
                     super(request);    
     }
     public String getParameter(java.lang.String name){return null;}
     public Map getParameterMap(){return null;}
     public Enumeration getParameterNames(){return null;}
     public String[] getParameterValues(java.lang.String name){return null;}
  }
%>
<%
  Enumeration e = request.getAttributeNames();
  while(e.hasMoreElements()){
     String attName = (String)e.nextElement();
     request.removeAttribute(attName);
  }
   Wrapper wrap = new Wrapper(request);
   wrap.setAttribute("NewAttribute", "someValue");
   RequestDispatcher dispatcher = application.getRequestDispatcher("/destination.jsp");
   dispatcher.forward(wrap, response);
%>
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 200 total points
ID: 18789935
I guess I could make it easier for anyone to test by posting the other pages I used.
The starter HTML is  
---------------------------------------------------
<html><body>
   <a href="http://localhost:8080/forward.jsp?name=rrz">go to http://localhost:8080/forward.jsp</a>
</body></html>
----------------------------------------------------
forward.jsp
-----------------------------------------------------------
<%
  request.setAttribute("location","California");
  RequestDispatcher dispatcher = request.getRequestDispatcher("requestWrap.jsp");
  dispatcher.forward(request, response);
%>
------------------------------------------------------
and then finally,  destination.jsp  
----------------------------------------------------------------
<%@ page import="java.util.*"%>
<html><body>
hello! from destination.jsp<br/>
name is ${param.name}<br/>
NewAttribute is ${NewAttribute}<br/>
location is ${location}<br/>
<%
  Enumeration e2 = request.getAttributeNames();
  while(e2.hasMoreElements()){
     String att = (String)e2.nextElement();
     out.print(att + "=" + request.getAttribute(att) + "<br/>");
  }
%>
</body></html>

0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18794300
william, any questions?
0
 
LVL 9

Author Comment

by:william007
ID: 18806509
No question at all, thanks for help:-)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18807898
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now