Solved

JSP - How to clear the HttpServletRequest

Posted on 2007-03-25
10
3,504 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Eclipse Neon and jdk 1.8.0 11 135
reasons why a "inside Menu" would not appear for a web server 6 56
Java Loop 6 49
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

23 Experts available now in Live!

Get 1:1 Help Now