Birdman
Birdman

Reputation: 5424

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);

Upvotes: 0

Views: 1420

Answers (2)

parsifal
parsifal

Reputation: 266

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.

Upvotes: 2

GriffeyDog
GriffeyDog

Reputation: 8386

You could redirect to the JSP instead of forwarding to it:

response.sendRedirect("index.jsp");

Upvotes: -1

Related Questions