redirect a servlet

A

autogoor

We have a couple of online resources (cgi, servlet...) on different
machine and even different domains. For example, user can call

http://www.domain1.com/app1&name="smith"
or
http://www.domain2.com/cgi-bin/app2&zip="94000"

and get response.

We would like to provide a central location which can take user's
request and forward them to appropriate destinations. Thus, if user
call:

http://www.centeral.com/mainapp?app=app1&name="smith"

The request will be forwarded to the first service.

I am writing a servlet which basically check user's authorization and
they forward the request with user's original parameters to other
resources.

Here is my code for centeral/mainapp:

public class MainAppServlet extends HttpServlet{
private static final String s1="http://www.domain1.com/app1";
private static final String
s2="http://www.domain2.com/cgi-bin/app2";
private static final String s3="http://www.domain3.com/app2";

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

//check if user loged in
...

//forward
String app = request.getParameter("app");
String des;
if ("app1".equals(app)){
des = s1;
}
if ("app2".equals(app)){
des = s2;
}

if ("app3".equals(app)){
des = s3;
}

response.senRequestDispatcher dispatcher =
request.getRequestDispatcher(des);
dispatcher.forward(request, response);
}
}
We found the code did not work. I think it is because "forward" can
only handle request to the same server. How can I fix it? I am thinking
about sendRedirect, but how can I forward parameters with it?

Thanks,

Autogoor
 
W

Wendy S

We found the code did not work. I think it is because "forward" can
only handle request to the same server. How can I fix it? I am thinking
about sendRedirect, but how can I forward parameters with it?

Yes, forward only works within the container. You send parameters with a
redirect the same way you got them-- in the URL.

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
See if request.getQueryString(...) is helpful.

Otherwise, request.getParameter(...) will work if you know the parameter
name you're looking for, or getParameterNames if you need all of them.
 
R

Ross Bamford

Yes, forward only works within the container. You send parameters with a
redirect the same way you got them-- in the URL.

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
See if request.getQueryString(...) is helpful.

Otherwise, request.getParameter(...) will work if you know the parameter
name you're looking for, or getParameterNames if you need all of them.

An easy way to do this would be to write a REFRESH header to the
response:

response.addHeader("REFRESH","5;url=http://www.whatever.com/");

I think that should work, if you just want to punt the client someplace
else :)

+-= Ross =-+
 
M

Minh Tran-Le

Ross said:
Yes, forward only works within the container. You send parameters with a
redirect the same way you got them-- in the URL.

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
See if request.getQueryString(...) is helpful.

Otherwise, request.getParameter(...) will work if you know the parameter
name you're looking for, or getParameterNames if you need all of them.


An easy way to do this would be to write a REFRESH header to the
response:

response.addHeader("REFRESH","5;url=http://www.whatever.com/");

I think that should work, if you just want to punt the client someplace
else :)

+-= Ross =-+[/QUOTE]

You could also do:
response.sendRedirect("http://www.whatever.com/xxx?other=args");

One thing that I don't know for sure is if you could redirect an HTTP POST.

Minh Tran-Le.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top