Erik Sapir
Erik Sapir

Reputation: 24777

How to allow access to static content when having default servlet

I map all requests to /* to a specific servlet. My static content is hidden by this configuration.

How can i allow access to specific files (such as crossdomain.xml)?

Upvotes: 3

Views: 708

Answers (4)

BalusC
BalusC

Reputation: 1109874

Cookbook:

  • Map your controller Servlet on a more specific url-pattern like /app/*.
  • Put all the static content in a specific folder like /static.
  • Create a Filter which is mapped on /* which transparently continues the chain for any /static requests and dispatches other requests to /app.

So, in a nutshell:

<filter>
    <filter-name>filter</filter-name>
    <filter-class>com.example.Filter</filter-class>
</filter>
<filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>com.example.Controller</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/pages/*</url-pattern>
</servlet-mapping>

with the following in filter's doFilter():

String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.startsWith("/static/")) {
    chain.doFilter(request, response); // Goes to default servlet.
} else {
    request.getRequestDispatcher("/app" + uri).forward(request, response);
}

No, you do not end up with extra /app path in the URL. It's fully transparent. Make if necessary "/static" and/or "/app" an <init-param> of the filter.

Upvotes: 1

Yohan Liyanage
Yohan Liyanage

Reputation: 7000

When you map /* to a specific servlet, all requests will be forwarded to that servlet, unless you provide a more explicit mapping to another servlet.

That is, if you have /* mapped to ServletA, and /static/* mapped to ServletB, then following Servlets will get called.

http://localhost:8080/abc.jpg            -> ServletA
http://localhost:8080/static/abc.jpg     -> ServletB
http://localhost:8080/xyz/abc.jpg        -> ServletA

So one option you have is to write a Servlet to handle the static content, which will grab the file and return it as response. You can map that servlet to a prefixed by something like /static/*. This requires that all URL references to your static files to be updated to contain this '/static' part.

If that is not feasible for you, then probably you can use the same servlet, but mapped to multiple URL patterns (probably by extension) as follows.

<servlet>
    <servlet-name>static-servlet</servlet-name>
    <servlet-class>xxx.yyy.StaticServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>static-servlet</servlet-name>
    <url-pattern>*.xml</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>static-servlet</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>

If you want this to be fine-grained to the level of each file, you can map the servlet to your file URL as well.

Upvotes: 2

Reddy
Reddy

Reputation: 8921

probably you can put your static content under different URL like /static/* and then map this URL to a Servlet which responds with the static content.

Upvotes: 0

Ramesh PVK
Ramesh PVK

Reputation: 15456

And one more(a direct) servlet mapping like this<servlet-mapping><servlet-name>StaticContentServlet</servlet-name><url-pattern>/crossdomain.xml</url-pattern></servlet-mapping>

Upvotes: 0

Related Questions