Solved

JSP - How to clear the HttpServletRequest

Posted on 2007-03-25
10
3,776 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:Mayank S
Mayank S 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:Mayank S
Mayank S 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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
 
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:Mayank S
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:Mayank S
ID: 18807898
:-)
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 398
Is Applet the way to go for my drag and drop system? 8 32
jsp error 6 42
Drop down list show all elements but disabled for selection 6 31
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

680 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