• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simulating "request.setRequest"

 
mik laan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Does Servlet API contain any methods to add parameter to querystring?
I have a situation where I should add key=value-pair to request that I am forwarding from one servlet to another.
How can this kind of thing be handled?
There are no request.setParameter or response.setParameter methods or I did not find them. And using request.setAttribute does not work in this case.
Is it possible to do a own new request and fill it with a new key=value-pair?
Can anyone help me?
Thanks,
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mik,

You didn't find those methods because they aren't there.

Filters are supposed to be able to let you manipulate the request and response. I haven't tried anything with those yet, and it might be non-trivial. But that's one option.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to do that setAttribute() won't accomplish? True, it won't add the data to the list of request parameters, but if all you are trying to do is to get the data forwarded, then it would work just fine.
So is there a specific reason that the data must be passed along as a request parameter?
bear
 
Gopi Balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mik laan:
Hello,
Does Servlet API contain any methods to add parameter to querystring?

No.

Is it possible to do a own new request and fill it with a new key=value-pair?

There are two ways.
1. Create a URL of your target servlet. Append all parameters and values already present in your request. To this, append your key-value pairs. And, redirect to the target. The target will find these new key-value pairs as parameters rather than as attributes.
2. If you have a servlet container, whose source you have access to (like say, Tomcat), you can look around, and change your source to be aware of Tomcat stuff. [Have a look at the Java file -
$TOMCAT_HOME_DIR$/src/catalina/src/share/org/apache/catalina/HttpRequest.java and the method addParameter()]. But, this will definitely make your servlet non-portable, and perhaps dependent on that particular Version of Tomcat.
-GB.
[ February 26, 2003: Message edited by: Gopi Balaji ]
 
Gopi Balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

So is there a specific reason that the data must be passed along as a request parameter?
bear

One reason I can think of is, when you're forwarding a modified request to a Servlet/ASP page/CGI script/etc.. in another application, perhaps in another domain, over which you have no source control.
-GB.
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mik laan:
Does Servlet API contain any methods to add parameter to querystring?
I have a situation where I should add key=value-pair to request that I am forwarding from one servlet to another.
How can this kind of thing be handled?

The easiest way would be to use a query string at the end of the path when you obtain the RequestDispatcher as:
RequestDispatcher rd = request.getRequestDispatcher("/OtherServlet?key=value");
rd.forward(request,response);
Originally posted by mik laan:
Is it possible to do a own new request and fill it with a new key=value-pair?

Yes, you can extend the HttpServletRequestWrapper class and override one or more of the getParameter(), getParameterNames(), ... methods. Something like this:
public String getParameter(String name)
{
if ("myKey".equals(name))
return "myValue";
return super.getParameter(name);
}
Hope that helps,
-j
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!