Issue
I am building a small website using JPA/Hibernate 5.4.2, Java Servlet API 4.0.1, and Tomcat 9 for my server. I am using Eclipse JEE 4.11 as my IDE.
Essentially, I've been having a lot of issues with Servlet and JSP mappings, and I can't understand why. In one instance, I had one particular JSP file that refused to acknowledge other JSP files, even though they are all in the same folder. For example, it could not see my header files (which were included using ) unless I added the folder name (e.g. "/jsp/Header.jsp"). However, doing so would then mess up my URLs for every other page, preventing them from loading the header and footer in turn.
As best as I can tell, it's as though the application switches directory to access a given file, but it somehow never reverts back, and so other URL paths become invalid, as they are now pointing to the wrong location. It's worth mentioning that this does not appear to work for my tags, where I've had to use relative URLs.
So far, I've managed to fix many of these issues by prefixing my URLs with ${pageContext.request.contextPath}, but I've run into a situation where my URLs still get messed up after visiting a specific page.
I've opted to use a web.xml file (located in "webapp/WEB-INF") to map my JSPs and Servlets, with a sample of each below. Note that the Servlet entries were generated by Eclipse, but I manually mapped my JSP files using the same format. I've double-checked the XML file to make sure I hadn't made any obvious errors, but I've yet to find any. The only thing I've found in my research is that the order of the mappings might be important, but no one ever really gives concrete information about it.
I've also tried cleaning my project in Eclipse, refreshing the project (as I've had issues with video files not being recognized by my application until doing so), and cleaning my Tomcat's working directory. None of that seems to have had any effect.
<servlet>
<display-name>ExerciseListServlet</display-name>
<servlet-name>ExerciseListServlet</servlet-name>
<servlet-class>web.ExerciseListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExerciseListServlet</servlet-name>
<url-pattern>/ExerciseListServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserProfile</servlet-name>
<jsp-file>/jsp/UserProfile.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>UserProfile</servlet-name>
<url-pattern>/UserProfile</url-pattern>
</servlet-mapping>
Any thoughts? I'm well aware that I haven't posted much in the way of code, but this problem has been occurring with just about every page at some point, and the fact that I didn't have this problem the last time I worked with these systems tells me that there may be a bug or bad configuration with either Tomcat, Hibernate or maybe Eclipse. At the moment, my system is throwing exceptions for completely unrelated reasons, though I believe I know why, so I won't include that for now.
Solution
You just have to remove all the contents of web.xml file. remove all the servlets and corresponding mappings of it.
Then you can make your Servlet to listen on specific url by adding WebServlet Annotation. Use below annotation in your code.
@WebServlet(urlPatterns = "/ExerciseListServlet", loadOnStartup = 1, displayName = "ExerciseListServlet")
@MultipartConfig
So whenever your servlet is use related paths and display name.
Answered By - Onkar Musale