Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JSP - How to clear the HttpServletRequest

Posted on 2007-03-25
10
Medium Priority
?
4,255 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 600 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 600 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 600 total points
ID: 18788994
Otherwise you can set the attributes to null but I doubt if you cannot change the values of parameters.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 28

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 28

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 28

Accepted Solution

by:
rrz earned 800 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 28

Assisted Solution

by:rrz
rrz earned 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

730 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