Default servlet and images/css/...

M

Marcin Okraszewski

Hi,
I want to make a web app that would use a default servlet. But when I
set it I can't access images, css, etc.

Is there any way to avoid this problem?

Or, maybe you have a better idea to make an completely dynamic site what
would have human-readable URLs? I mean something like nukes that you
define whole site by a web page but in this case I want to make it look
like a site with a directory structure.

I'm using Tomcat 4.1.24. Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>DefaultServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>

<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>


Regards,
Marcin Okraszewski
 
J

John C. Bollinger

Marcin said:
I want to make a web app that would use a default servlet. But when I
set it I can't access images, css, etc.

I don't know exactly what you mean by that. My guess is that you mean
that client requests for the corresponding URLs fail; this is a natural
consequence of defining a default servlet for the web application. When
a request arrives and the servlet container has no other _servlet_
(which includes JSP-derived servlets) to which to map that request, it
is mapped to the default servlet. Static content does not enter the
mapping picture. In the presence of a default servlet, static content
belonging to the webapp must be delivered by a servlet in the webapp.

The alternative, more likely if you already understood the above, is
that you mean that your default servlet cannot access the files
containing the static content. If this is the case then it will help if
you post some code from a servlet that exhibts the problem.
Is there any way to avoid this problem?

It depends on exactly what behavior you want. There are ways to allow
static content to be served from your webapp, while still applying a
servlet to URLs that do not match any other resource. For instance,
instead of defining a default servlet, you could use an error-page
element to direct to your "default" servlet in the case of a 404 error
(but then watch out for infinite loops). If using a servelet container
with a web server in front of it (e.g. Tomcat w/ Apache) then you could
use the web server configuration to filter out and serve requests for
static resources. But either of those takes you away from your design
goal of using one servlet as a gateway to everything.

In order to use the one-servlet gateway to everything approach, you must
make your default servlet able to deliver everything. Again, if that is
where you have encountered problems then post some code for it.
Or, maybe you have a better idea to make an completely dynamic site what
would have human-readable URLs? I mean something like nukes that you
define whole site by a web page but in this case I want to make it look
like a site with a directory structure.

The directory structure bit doesn't have much to do with the question,
really. It helps identify the requested resource, but your problem is
not with identifying the resource but with delivering it.
I'm using Tomcat 4.1.24. Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>DefaultServlet</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>

<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>


John Bollinger
(e-mail address removed)
 
W

William Brogden

Marcin Okraszewski said:
Hi,
I want to make a web app that would use a default servlet. But when I
set it I can't access images, css, etc.

Is there any way to avoid this problem?

Thats probably because you are using relative addresses for those resources.
The browser sees the URL of the servlet and tries to use that as a base for
requests for the CSS, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top