0

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);
1
  • The servlet's in your URL because you originally sent your request to it (I assume). If you want it to disappear, the browser will have to perform another request. What do you mean by "losing functionality of the Servlet"? If you redirect, the servlet has already been executed. Commented Sep 12, 2012 at 14:08

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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

response.sendRedirect("index.jsp");

2 Comments

Not redirecting is exactly what the question is about.
I somehow missed that in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.