Issue
I have a project, built from a tutorial, in progress. It uses Hibernate and Java Servlets, locally, on a Tomcat server. I get different results with the following two urls:
- localhost:8080/MyProject/admin
- localhost:8080/MyProject/admin/
Either results in the display of the page login.jsp
via forwarding executed in an implementation of Servlet.Filter
:
RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/login.jsp");
dispatcher.forward(request, response);
But the two pages seem to think they are at differing addresses. Within the JSP page is a link for the css file.
<link rel="stylesheet" href="../css/styles.css">
When I view the "page source" for the two URLs, here are the addresses that show up when mousing over the above links:
localhost:8080/css/styles.css
localhost:8080/MyProject/css/styles.css
So, the styles.css
file is only found if I use the address localhost:8080/MyProject/admin/
.
Is there a way to have the Servlet.Filter
set the correct/intended address such that a user can either add or omit the "/" and still arrive at the same location with the same functionality?
I'm having trouble locating syntax info pertaining to the "/"--and would love to have a link to background info or explanations.
Solution
From an article I found, it looks like a folder can either be addressed with a "/" at the end or not. It's not a settled situation. The main thing is to be consistent.
Since the form with the slash at the end is being interpreted correctly by the Servlets code, and the other isn't, I have reached the conclusion that the best plan would be to stick with the slash version, and use 301 redirects to handle the non-slash case.
Answered By - Phil Freihofner