Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stylesheet doesn't load in JSP  RSS feed

 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Novice here, couldn't figure out what I am doing wrong. I invoke a servlet, processes and 'forward()'s to another jsp. This jsp has a stylesheet reference ( path is correct). When thru forward, the stylesheet doesn't seem to apply, but when I directly call the jsp from teh browser, the stylesheets applies.

Is there any header settings I need to take care fo in the servlet before I do the forward???

I know pasting some code helps, but this not a code issue apparently. I am sure missing a config parameter sorts in response object before I forward to the jsp?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Despite your confidence, I suspect that your path is not correct.

Are you using a path relative to the JSP page? Won't work.

In the servlet environment you need to specify paths to external resources such as images, scripts and stylesheets as server-relative paths, not page-relative paths.
[ September 11, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example:


(Assuming you are using JSP 2.0)
[ September 11, 2007: Message edited by: Bear Bibeault ]
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You rock.. That is correct, it worked when I replaced the relative path with what you gave. BUT I didn't get this....
http://localhost/s1/Channels
is processed by a servlet and forward is channels.jsp which has css link as "css/stylesheet.css"
-- works fine

Channels page has list of records, you click ( onCick="ClearToProceed('Channel.do/?channel_id=1');")
on one will take you to the Channel detail page. Again same mechanism,a servlet handles the request and forwards to the channel.jsp page WHICH HAS the same stylesheet link as the Channels.jsp

-- and this doesn't work...

HOWEVER, if I directly do a http://localhost/s1/channel.jsp in the browser, it works fine..

WHY WHY WHY
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, you are using incomplete URLs. All client-side URLs should be server-relative rather than page relative. The page relatives URLs may sometimes work by chance depending upon what the servlet path is, but it's hit or miss.

Bottom line: if the the request is being initiaited from the browser (images, CSS, scripts, form actions, links) make the URLs complete, server-relative URLs and you will have no problems.

Page-relative URLs, even when they happen to work, are also fragile as they depend upon the relationsiup between the various resources staying static. Start moving files around, and you've got a mess to clean up.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to check out the <c:url> JSTL action for help in building URLs.
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Bear, I didn't know urls in jsp can be this tiresome. I will readup more.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is, of course, that the browser bases all page-relative URLs off what it thinks is the "current folder" that corresponds to the loaded URL. And when that's a servlet, there isn't any real folder that corresponds. So things get mucked up.

Using server-relative URLs doesn't give the browser a chance to get confused.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!