简体   繁体   中英

How to show .jsp extension in URL when forwarding from Servlet to JSP page

As the title says: How do I show the .jsp extension in the URL when forwarding from Servlet to a JSP page without redirecting and thereby losing functionality of the Servlet ?

If I forward from a Servlet to a JSP page by using the following code I still have the Servlet in my URL:

request.getRequestDispatcher("index.jsp").forward(request, response);

You don't.

The browser displays the URL that it thinks it's getting data from. Unless you tell it that it's getting a different URL via a redirect, it has no way to know what the server is doing behind the scenes. Nor should it.

From an application design perspective, you shouldn't care either. The only reason to use a unique URL is so that the user can set a bookmark. If the JSP page needs data from the servlet, then it doesn't make sense to give the user a different URL.

If you're dead set on giving the browser a ".jsp" URL, then have the servlet store data in the session. That's ugly, pointless, and subject to concurrent access issues, but it works.

您可以重定向到JSP而不是转发到它:

response.sendRedirect("index.jsp");

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