• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlets and CSS

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'll give these a try!
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic