In a typical web application server, there can be applications deployed to
several contexts within the container.  Each context is differentiated using a
URL prefix, e.g.:

http://myhost/webstore
http://myhost/benefits-admin

Webstore and benefits-admin represent different Servlet contexts which are
different applications served out of the same container.  In most cases, these
applications won't share resources, so the css files for the webstore
application will be somewhere below the document root for webstore, and the css
files for benefits-admin will be under it's document root, e.g.:

/deploy/webapps/webstore/styles/main.css
/deploy/webapps/benefits-admin/styles/main.css

When you refer to the main.css file for the webstore app, it's URI is
http://myhost/webstore/styles/main.css, or shortened to
/webstore/styles/main.css.  For benefits-admin, it's
/benefits-admin/styles/main.css.

The prefix part (/webstore, or /benefits-admin) is the "context path" and it is
defined in your application deployment descriptor.  This can be changed during
deployment, so you don't want it hard coded in your HTML, JSP, or Servlets.

It is possible to use relative paths to get to the URI, for example a servlet
whose URL is /webstore/servlet/MyServlet can refer to the css file as
../styles/main.css, however that means that if you map a different URL to your
servlet, the relative URL won't work anymore.  Also, take an example of a JSP
found at /webstore/cart.jsp.  The relative URL of styles/main.css would work,
but what happens if the cart.jsp is redirected to from a servlet using a
RequestDispatcher?  The browser thinks the URL is
/webstore/servlet/DirectorServlet, so the relative URL breaks!

Explicity referring to the context path and appending the absolute path to the
resource within the deployed web application ensures that the reference will
always work, so long as the resource is in the location specified by the
absolute path.

Obviously this is all irrelevant if you only have one context in your
application server and it is deployed to the web site root.

John H.




"Robert Koberg" <[EMAIL PROTECTED]>@orionserver.com on 06/21/2001 01:28:51 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


> To be safe, you really ought to use request.getContextPath() to prefix the
> absolute URL of your css file relative to your document root.

why (just curious)?

what is different if just use:

<link rel="stylesheet" type="text/css" href="/blah.css" title="blah">

The above is more generic (you don't need to run JSP for every file)




----- Original Message -----
From: <[EMAIL PROTECTED]>
To: "Orion-Interest" <[EMAIL PROTECTED]>
Sent: Thursday, June 21, 2001 7:29 AM
Subject: RE: Aaarrrghhh!! CSS and Servlet again


>
>
> 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