简体   繁体   中英

How to redirect a HTTPRequest from one controller class to other controller class?

I have two controller. One is main controller and other is intermediate controller. In intermediate controller I should add header to HTTPRequest. After Adding I should redirect it to main controller where I should check the header I added. How can I do this? Can anyone help me for this?pls........

Seems like a good place to use Servlet Filter . If you want to pre-process an incoming request you could do this with servlet filters and then simply chain it to the appropriate servlet .

Refer BalusC Answer for details .

If this is not the case your can merely forward the request like :

        request.getRequestDispatcher("/yourServlet").forward(request, response); // forward to the main servlet

Forward Dispatching

getServletContext().getRequestDispatcher().forward("second page");

The request forwarding is done internally by the JSF Controller Servlet to another resource. The browser is unaware of what has happened in the server side at the web container. So it still thinks it is tending to the original request and displays the original URL in its address bar. However, the page content displayed is from the second page.

Redirect Dispatching

response.sendRedirect("second page");

In this case, the JSF Controller Servlet instructs the client browser (via HTTP response header) to fetch another URL. So the browser fetches entirely a new URL and displays the second URL in its address bar. This could cause slight performance delay

from here

I think you need to forward the request rather than redirecting.

RequestDispatcher dispatcher= request.getRequestDispatcher("servlet-mapped-url");
dispatcher.forward(request, response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM