To be safe, you really ought to use request.getContextPath() to prefix the
absolute URL of your css file relative to your document root.  When you deploy
to a context like:

http://myhost:myport/mycontext

All of your paths are prefixed by /mycontext.  But if you change your deployment
descriptor, you might change "mycontext" to something else.  You don't want that
hard-coded into your servlets.  Also, the suggestion by elephantwalker is true -
you can use relative paths (/mycontext/servlet/../blah.css will get you to
/mycontext/blah.css) but if you decide to put your servlet in a package, or map
a different URL pattern to it (e.g. /mycontext/MyServlet) this won't work.
Using request.getContextPath() plus an absolute path is guaranteed to work so
long as the resource stays where you put it relative to the document root.  It
also comes in very handy when you put the reference in a JSP - sometimes the JSP
will be called directly (so the browser URL is /mycontext/myjsp.jsp) but
sometimes you get there using a RequestDispatcher, so the browser thinks the URL
is /mycontext/servlet/MyServlet.  In this case, relative URLs don't work.

So, put the css file in your document root and use:

<link rel="stylesheet" type="text/css" href="<%= request.getContextPath()
%>/blah.css" title="blah">

if your JSPs and

out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\" " +
request.getContextPath() + "/blah.css\" title=\"blah\">");

in your servlets.

John H.





"elephantwalker" <[EMAIL PROTECTED]>@orionserver.com on 06/20/2001
05:55:45 PM

Please respond to Orion-Interest <[EMAIL PROTECTED]>

Sent by:  [EMAIL PROTECTED]


To:   Orion-Interest <[EMAIL PROTECTED]>
cc:

Subject:  RE: Aaarrrghhh!! CSS and Servlet again



The  url pattern for your servlet is the issue. Somehow it is "/", which means
just  about everthing. I would use something different like, /employees or
/employees.html. Your path to the css should be just "../blah.css", and include
the blah.css in your war file at the root.

regards,

the  elephantwalker







 -----Original Message-----
From:  [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Brynolf  Andersson
Sent: Wednesday, June 20, 2001 3:28 PM
To: Orion-Interest
Subject:  Aaarrrghhh!! CSS and Servlet again


Hi all again,
I have been trying all lot of different  ways to pick uppthe CSS file but
without any success. So I'll try to see if  anyone has some more thing to add to
my problem.

The question is still there, where should I put my CSS file and how should  I
pick it up from the servlet.

Som information:
server.xml:
<application name="Lab01"  path="C:\oracle\JDeveloper 3.2.3\myprojects\lab_01"
/>

default-web-site.xml:
<!-- LAB 01 -->
<web-app  application="Lab01" name="web" root="/lab01" />

web.xml:
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application
2.2//EN"  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd";>

<web-app>
<display-name>Lab  01</display-name>
<description>desc</description>
<servlet>
<servlet-name>EmployeeServlet</servlet-name>
<servlet-class>training.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>training.EmployeeServlet</servlet-name>
<url-pattern>/</url-pattern>  -->
</servlet-mapping>
</web-app>

application.xml:
<?xml version="1.0"?>
<!DOCTYPE application  PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application
1.2//EN"  "http://java.sun.com/j2ee/dtds/application_1_2.dtd";>

<display-name>Lab01</display-name>
<module>
<web>
<web-uri>web</web-uri>
<context-root>/</context-root>
</web>
</module>
</application>

Directory structure of the  WEB-APP:
_lab_01
|_META-INF
|  |_application.xml
|_web
|____|WEB-INF
|____________|web.xml
|____________________|classes
|____________________________|pkg
|________________________________|myServlet


Help



Get your FREE download of MSN Explorer at http://explorer.msn.com



Reply via email to