tags:

views:

264

answers:

2

I've read several docs and I don't get it: I know I'm doing something wrong but I don't understand what. I've got a website that is entirely dynamically generated: there's hardly any static content at all.

So, trying to understand JSP/Servlet, I've written my own "front controller" intercepting every single query, it looks like this:

<servlet-mapping>
        <servlet-name>defaultservlet</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>

Basically I want any user request, like:

  • example.org
  • example.org/bar
  • example.org/foo.html

to all go through a default servlet which I've written.

The servlet then examines the URI and find to which .jsp the request must be dispatched, and then does, after having set all the attributes correctly, a:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
dispatcher.forward(req, resp);

When I'm using a url-pattern (in web.xml) like, say, *.html, everything works fine. But when I change it to /* (to really intercept everything), I enter an endless loop and it ends up with a... StackOverflow :)

When the request is dispatched, is the URI ".../WEB-INF/jsp/index.jsp" itself matched by the web.xml filter /* that I set?

EDIT apparently, no, because this is an exact mapping to index.jsp and hence it bypasses the web.xml url-pattern. So I still don't get how I can enter that endless loop.

How should I do if I want to intercept everything using a /* url-pattern and yet be able to dispatch/forward/?

I'm not asking about specs/Javadocs here: I'm really confused about the bigger picture and I'd need some explanation as to what could be going on.

Am I not supposed to intercept really everything?

If I can intercept everything, what should I be aware of regarding forwarding/dispatching?

A: 

Try using struts in which front controller pattern is inbuilt. There you will have a action class and you can define forwards in struts-config file using which you can easily manage the forwards.

Go through the tutorial http://www.roseindia.net/struts/struts2/index.shtml. Hope this helps you.

RaviG
@RaviG: I'm not after a framework. I'm trying to learn JSP/Servlet as stated in my question. In addition, I'm under the impression that Struts is a bit like last-century technology and that people starting from scratch should invest in frameworks like Spring MVC etc. :)
NoozNooz42
Try using * . * (without spaces) in your url-pattern.
RaviG
Besides, roseindia.net is a [horrible](http://balusc.blogspot.com/2008/06/what-is-it-with-roseindia.html) source. It only teaches bad practices. Don't use that site as learning source.
BalusC
+3  A: 

Unfortunately, Serlvet spec doesn't allow to create a servlet mapping to match only incoming request, not forwards. However, this can be done for filter mappings (and by default filter mappings match only incoming requests).

So, the typical solution for intercepting everything with a single servlet is to use a UrlRewriteFilter:

<filter>
    <filter-name>urlRewrite</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>urlRewrite</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>application</servlet-name>
    <servlet-class>...</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

/WEB-INF/urlrewrite.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
    "http://tuckey.org/res/dtds/urlrewrite3.0.dtd"&gt;

<urlrewrite default-match-type="wildcard">
    <rule>
        <from>/**</from>
        <to>/app/$1</to>
    </rule>
    <outbound-rule>
        <from>/app/**</from>
        <to>/$1</to>
    </outbound-rule>    
</urlrewrite>

This way also allows you to specify exceptions from /* mapping for static files.

axtavt