Solved

JSP - How to clear the HttpServletRequest

Posted on 2007-03-25
10
3,888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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 …
Suggested Courses

734 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