• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Mapping to Subdirectory/SubURL Doesn't See .JSP's -- 404 Error  RSS feed

 
Wayne Woodrow
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I have been working on a web application for some time and it's coming along, but I have encountered an issue and I don't know how it should be resolved. I'll try to keep it simple while hopefully providing enough information.

What I have done is created a Servlet with the following mapping "/Users/ViewAccount".
The Servlet is working properly, but the problem is that when I forward the request to my jsp, the Servlet is looking in "/Users" for the jsp, css and js files. How can I make it so the Servlet searches in the root web folder? All of the other Servlets so far do not specify a subfolder, their mappings are "/Register", "/SignIn", "/Tickets", etc.. so I haven't had the problem until now.

For the request dispatcher, this is the line I have been using:
RequestDispatcher rd = request.getRequestDispatcher("jsp/view_account.jsp"); -- 404 Error
RequestDispatcher rd = request.getRequestDispatcher("/jsp/view_account.jsp"); -- Loads the jsp page, but still looks for the css and js files in the "/Users" directory so I lose my formatting.

I have been using annotations for mapping my Servlets to URL's so my web.xml is mostly empty.

Is there an easy way to fix this?

Thank you for any help or comments.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66153
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Wayne Woodrow wrote:RequestDispatcher rd = request.getRequestDispatcher("/jsp/view_account.jsp"); -- Loads the jsp page, but still looks for the css and js files in the "/Users" directory so I lose my formatting.


I assume from this that the JSPs are in a folder named jsp?

Firstly, as they should not be able to be accessed except thought your servlet, you should move them to under WEB-INF. I would simply move the jsp folder to WEB-INF and adjust your dispatcher paths accordingly.

Secondly, with regards to browser resources (CSS and JS), use server-relative paths that start with the context path. See the JspFaq for information on how to do that programmatically.

Hint: precede the URLs with ${pageContext.request.contextPath}
 
Wayne Woodrow
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply Bear! Should all the folders for my web resources be moved into the WEB-INF as well (js, css, fonts, img)? I still need to look over the JspFAQ, I didn't completely understand the last part. In the meantime, I have attached a screenshot of the project structure.
structure.PNG
[Thumbnail for structure.PNG]
Project file structure - Netbeans
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wayne Woodrow wrote:Should all the folders for my web resources be moved into the WEB-INF as well (js, css, fonts, img)?


No, definitely not. WEB-INF is for resources which you don't want the browser to be able to access, but all of those things you named there are accessed by the browser.
 
Wayne Woodrow
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information on the file paths, it is now working correctly.

Regarding the JSP's in the WEB-INF folder.. I have moved them all into the WEB-INF folder, however using the RequestDispatcher my "/Welcome" Servlet is not able to see the jsp using the following path:
"WEB-INF/jsp/welcome.jsp"

I understand now that WEB-INF isn't directly accessible by the browser, but shouldn't the Servlet be able to access it? What am I missing?

Thanks!
Capture.PNG
[Thumbnail for Capture.PNG]
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using a relative URL for those JSPs, you should use a context-relative path in that situation. Have a look at the JspFaq page (follow the link in Bear's post).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66153
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: leading slash, cough cough
 
Wayne Woodrow
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, that part is working now. I kind of ran into the same problem again, though. The welcome.jsp file uses tags from global_layout.tag. Within the tag file, I am using <jsp:include> directive to include separate jsp's for the navbar and sidebar. I read through the Servlet 3.0 specification but I can't tell if this is an issue with the jsp accessing another jsp inside WEB-INF, or something else..
Capture3.PNG
[Thumbnail for Capture3.PNG]
Capture.PNG
[Thumbnail for Capture.PNG]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66153
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, on the surface I'm not seeing what's going wrong there. But's it's been a while once I've done such an include.

Docs say that it wants a relative URL, so as a first step I'd try removing the leading slash.
 
Wayne Woodrow
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for a test, I copied the file 'default_page.jsp' into the 'navbar' and 'sidebar' folders and then updated 'global_layout.tag' to include those pages instead. The 'default_page.jsp' only contains HTML, and this runs with no problem. If you'd like to take a look, here are the contents of 'navbar.jsp' and ''sidebar.jsp'. I guess there's something in those files it doesn't like.. However, they had been working before.

navbar.jsp


sidebar.jsp
Capture.PNG
[Thumbnail for Capture.PNG]
 
Wayne Woodrow
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edited..
Everything is now working. For some reason the changes I was making weren't being reflected property, so I had to redeploy the application and it's now working. The paths to the jsp's are now of the form "/WEB-INF/jsp/..." Thank you for your time and assistance.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66153
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our pleasure! Fantastic that you've got things working!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!