Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Filter causing JSPs to fail to load with CSS/Javascript/Images  RSS feed

 
Jake Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm just starting out with Java EE and I have a basic web application with the following directory structure

java
|------
webapp
|------js
|------img
|------fonts
|------css
|------restricted
|------WEB-INF

I have implemented a Servlet Filter class to allow only logged in users to access certain pages. Such pages are JSPs stored in the 'restricted' folder. My problem is that these JSPs fail to load with the CSS, Javascript or images while in this folder with the Filter in place.

My doFilter method is as follows:



& I've included the filter mapping in the web.xml as follows



What changes can I make to my code to make the pages load with the CSS, javascript and images? Any help is greatly appreciated, thanks.



 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be a little specific what is happening, whether its printing stack trace in the browser/server log, if yes, post the stack trace.
I would like to see how CSS/JS are included in one of the JSP in "restricted" directory
 
Jake Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:Please be a little specific what is happening, whether its printing stack trace in the browser/server log, if yes, post the stack trace.
I would like to see how CSS/JS are included in one of the JSP in "restricted" directory


Thanks for the reply.

The restricted JSP pages are only showing text.

The CSS/JS is included in the pages as follows:

 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think you are missing the trick here
"restricted" directory is parallel with "js" and "css" directories.
So it is required to move 1 directory up from "restricted" before entering into the "css" directory. Something like this
 
Jake Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:
I think you are missing the trick here
"restricted" directory is parallel with "js" and "css" directories.
So it is required to move 1 directory up from "restricted" before entering into the "css" directory. Something like this


Perfect! This sorted it out. Thank you very much.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Thomas wrote:
Perfect! This sorted it out. Thank you very much.

While that worked for you, it's a fragile solution. Your resource URLs. rather, should be server-relative, starting out with the context path. This is outlined in detail in the JspFaq.

I would highly recommend following this best practice rather than relying on page-relative paths that are easily broken.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bear, the JspFaq is quite helpful
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!