javaweb-applicationshttp-redirectcross-context

How to pass data when redirecting from one web application to another?


I have two web application which are deployed in the same J2EE Application server. I need to redirect from one web application to another. Also I need to set some information in the Header from web application 1 so that it is available in web application 2. When I try to access the information which set in the header of Web App1 in Web App2, I'm getting "null". I'm using response.sendRedirect("http://localhost/webapp2") in Web Application 1 to redirect. Please help me to solve this.

Thanks, Apps.


Solution

  • A redirect is processed by the client (the browser). So only the client gets the headers you sent. The headers will not be passed to the webapp redirected to.

    You can do one of the following things to pass information from one webapp to another:

    A cookie received by the client will be send back to the server. The client does not know it is a different webapp. You just need to set the cookie path to /.

    Cross context dispacthing is done by an internal forward in the container (use a RequestDispatcher from the ServletContext). The client will never know, the request is handled by another webapp. You than can set a request attribute to pass data. Cross context dispatching has to be enabled by the container for security reasons.