Reg redirection
Hi,
I have two servlets which is running on two different web applications. When i hit a servlet it has to forward to another servlet which is running on different web application. i tried request.getRequestDispatcher and servletContext.getRequestDispatcher but both takes as relative URL and appends to the existing URL. If i use sendredirect my request is lost but i need the request which has to be processed at the other end.
How to change the context path through code so that when i hit the servlet it points to the context path of my another web application.
Please help me about this.
Regards,
Sangee
# 1
To d a forward to another application running in the same server you'll ned to get the servlet context object for the other web app using the ServletContext.getContext method.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getContext(java.lang.String)
And from this ServletConext you can get the ReqestDispatcher. Your Server will need to support cross context access and in Tomcat it needs to be enabled in both web apps.
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
A couple alternatives:
1: You can include the request parameters in the url in the response.sendRedirect and they will be included in the request sent to the second web app.
2: Instead of a redirect forward the request to a JSP that has a form with the needed request parameters. Populate the form and then have a JavaScript function that automatically submits the form when the form is done loading. The JavaScript submits the form to the second web app.
3: In the first web app use the Apache Jakarta Commons HttpClient package to submit the HTTP request to the second web app with all of the needed parameters and stream the response of the second web app back to the browser.