Servlets, send redirect and request scope

B

Berlin Brown

In a typical (MVC1) application. You have a HTML-FORM posting to
servlet-A and getting request parameters/values from the request
object.

HTML-FORM ==> Servlet-A

If I have to put a redirect in between the form and A. The request
values are lost. Is there a generic way to restore those request
values. I could put all the keys/values in a session object and then
recollect the values in the servlet. But, I wish there was a generic
way that possibly avoided using the session object.

HTML-FORM ==> response?.sendRedirect ==> Servlet-A

Something like response.putRequestValue...
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Berlin said:
In a typical (MVC1) application. You have a HTML-FORM posting to
servlet-A and getting request parameters/values from the request
object.

HTML-FORM ==> Servlet-A

If I have to put a redirect in between the form and A. The request
values are lost. Is there a generic way to restore those request
values. I could put all the keys/values in a session object and then
recollect the values in the servlet. But, I wish there was a generic
way that possibly avoided using the session object.

HTML-FORM ==> response?.sendRedirect ==> Servlet-A

Something like response.putRequestValue...

Why not use forward instead of redirect ?

(that will keep request info)

Arne
 
B

Berlin Brown

Yea, but it isnt as clean and not generic. For example, I would have
find every instance of request 'getParameters' that existed at the end
servlet and replace with the session getting code.

I wish there was a way to fill in values in the request object, that
way I wouldnt have to change any code at all.
 
C

Chris Smith

Berlin Brown said:
Yea, but it isnt as clean and not generic. For example, I would have
find every instance of request 'getParameters' that existed at the end
servlet and replace with the session getting code.

I wish there was a way to fill in values in the request object, that
way I wouldnt have to change any code at all.

And with a modern servlet spec, there is! You'll still have to get the
new information to the new request somehow. Once it's there, though,
you can set up a servlet filter that builds a wrapped ServletRequest
object and adds parameters and/or request attributes. Google on servlet
filters for details.
 
B

Berlin Brown

How would I do that. I know about filters. But what about in this
context.

Would I use the redirect to hit a 'filter'.
 
C

Chris Smith

Berlin Brown said:
How would I do that. I know about filters. But what about in this
context.

Would I use the redirect to hit a 'filter'.

Yes. For example, you might use something like this:

1. Set a session attribute before you redirect containing the desired
request attributes and parameters.

2. Install a filter that intercepts all requests.

- If the session attribute doesn't exist, then just pass the request
along the chain without doing anything.

- If the session attribute does exist, then build a wrapped request
that returns the desired attributes and parameters, and pass that
along the chain.
 
B

Berlin Brown

What you are saying makes sense, except for the last part. Let me add a
little bit more pseudo code (note I have some of my code in there):

public class TestReconnectServlet extends BaseServlet {
doPost() {

String theKey = request.getParameterKey();
String theValue = request.getParameter(theKey);
Object arr [] = new Object[2];
arr[0] = theKey;
arr[1] = theValue;
session.setValue("mysession.before.redirect", arr);
response.sendRedirect(GoToFilter?);

}
}
public class TheFilter implements Filter {
public void doFilter(ServletRequest reqBasic, ServletResponse
respBasic, FilterChain chain) {

HttpServletRequest req = (HttpServletRequest) reqBasic;
HttpServletResponse res = (HttpServletResponse) respBasic;

/// Here ????????? build up the request object?
String key, value = session.getValue(arr);
request .setValue(key, value);

...

chain.doFilter(req, res);

}
}


Hmm, this might actually work?

response.sendRedirect(GoToFilter?);

When I do the redirect, it is to the GoToFilter?
 
C

Chris Smith

Berlin Brown said:
/// Here ????????? build up the request object?
String key, value = session.getValue(arr);
request .setValue(key, value);

Check the API docs for javax.servlet.http.HttpServletRequestWrapper.
You'd build a subclass of that, and pass it to FilterChain.doFilter in
place of the original ServletRequest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top