How to redirect Servlet to given URL
Currently, I am using
request.getRequestDispatcher("thePage.开发者_Python百科html").forward(request, response);
in my servlet to the user the new page. But the URL of the servlet stays in browser address bar. I want the URL of the target page to be shown in browser address bar, instead of the initial servlet URL. How do I accomplish this?
You can do response.sendRedirect("thePage.html")
, but then that page needs to be directly accessible from the Internet. In particular, it can be accessed directly without going to the servlet first. It will also incur an additional roundtrip (whereas a forward just returns the result within the same request-response cycle).
Depending on what you are trying to do, you should probably also look at Servlet Filters and the possibility to associate any name (including "thePage.html" and path prefixes) to a Servlet.
精彩评论