Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets and CSS

 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
When the Servlet generates HTML, I need to direct the HTML page to use a style sheet located in myAppRoot/jsp/style_sheets, where myAppRoot is the root of my web application (using Tomcat). Now, the servlet is located in WEB-INF/classes/myServlet. I'm having trouble forming the path (see the LINK tag in the code below). This code, as is, cannot find the stylesheet.



I can put an absolute path and it will work, but this won't be good for the application. So, what is the path of the Servlet-generated HTML page? It's the same as the Servlet, right? This is what I thought, so I form a path to go up 2 directories (../../), find the style_sheets directory, and ultimately my style sheet. But it's not working.

Thanks for your attention!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can put an absolute path and it will work, but this won't be good for the application.


Why not?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer my own question, it is a very bad practice to hard-code the context path into URLs. But you can easily get around that.

Tactic 1: Form URLs using something like:

<link href="${pageContext.request.contextPath}/styles/mystyles.css" ...

Tactic 2: Dynamically generate a base tag:

<base href="${pageContext.request.contextPath}"/>

from which you specify relative URLs.


Note that the above examples use JSP 2.0 features. They are also feasible in JSP 1.2 using scriplet expresssions.
[ July 21, 2004: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also investigate the <c:url/> JSTL tag.

[Aside: Personally I've written a set of custom actions (tags) that dynamically figure out where things are at run-time so that I can easily "skin" my sites.]
[ July 22, 2004: Message edited by: Bear Bibeault ]
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll give these a try!
 
Richard Bradford
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"<LINK rel='stylesheet href='style_sheets/room_ride_share.css'>"

The above should work fine. Does for me at least.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But will only work if style_sheets is a folder located relative to the current URI, which is not the case under discussion.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
<base href="${pageContext.request.contextPath}"/>


Thanks, this works. Of course I've changed the design now, haha. I'm shying away from generating HTML in the Servlet (unless it's very basic). Instead, I'm using request.setAttribute() to set any applicable attributes, and redirecting to a JSP for display to user.

Slowly but surely, all these components fall into place.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic