| Author |
Servlets and CSS
|
Jeffrey Hunter
Ranch Hand
Joined: Apr 16, 2004
Posts: 305
|
|
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
Joined: Jan 10, 2002
Posts: 56228
|
|
I can put an absolute path and it will work, but this won't be good for the application.
Why not?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56228
|
|
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
Joined: Jan 10, 2002
Posts: 56228
|
|
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
Joined: Apr 16, 2004
Posts: 305
|
|
|
Thanks, I'll give these a try!
|
 |
Richard Bradford
Ranch Hand
Joined: Apr 20, 2004
Posts: 48
|
|
"<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
Joined: Jan 10, 2002
Posts: 56228
|
|
|
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
Joined: Apr 16, 2004
Posts: 305
|
|
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.
|
 |
 |
|
|
subject: Servlets and CSS
|
|
|