Re: servlet annotations for URL mapping?

Discussion in 'Java' started by Mike Schilling, May 1, 2008.

  1. Rex Mottram wrote:
    > Rex Mottram wrote:
    >> And replace it annotations in the servlet itself.

    >
    > Meant "replace it _with_ annotations ..." of course.


    And the container, rather than reading web.xml, looks at every class
    file in the webapp to see if it's annotated as a servlet? That sounds
    like a bad idea.

    {Why "Rex Mottram", by the way? He's not particularly a sympathetic
    character.)
    Mike Schilling, May 1, 2008
    #1
    1. Advertising

  2. On Apr 30, 10:58 pm, Rex Mottram <> wrote:
    > Mike Schilling wrote:
    > > Rex Mottram wrote:
    > >> Rex Mottram wrote:
    > >>> And replace it annotations in the servlet itself.
    > >> Meant "replace it _with_ annotations ..." of course.

    >
    > > And the container, rather than reading web.xml, looks at every class
    > > file in the webapp to see if it's annotated as a servlet?  That sounds
    > > like a bad idea.

    >
    > Maybe so, but it's a bad idea which is mandated by the latest revision
    > of the servlet spec. There are certain limitations - I believe it is
    > only required to examine the class files under WEB-INF/classes and in
    > jar files under WEB-INF/lib, and there's a way to turn off the search if
    > you don't want it.


    Not just the servlet spec, either: classpath scanning for annotations
    is a fairly well-established practice at this point. It's mandated
    (indirectly) by the EJB 3 and JPA specifications, which allow for
    descriptor-free deployment of EJBs and mapped entities respectively;
    it's also used in a number of third-party frameworks, including
    notably Spring 2.5 and Struts 2.

    Looking at the servlet 2.5 spec, I don't see any annotations mentioned
    that could be used to control servlet deployment (as, eg, @Stateless,
    @Stateful, @Local, and @Remote are used to control EJB deployment).
    This is consistent with the servlet 2.5 API docs, which contain no
    annotation types. I believe you're out of luck, at least until the
    next servlet specification, if you want to stay within Sun's
    specification constellation.

    However, I've had extremely positive experiences using annotations to
    configure Spring Web MVC. See <http://static.springframework.org/
    spring/docs/2.5.x/reference/mvc.html#mvc-annotation> for details; it
    can be deployed inside any servlet container with little difficulty
    and does allow for annotation-driven configuration.

    -o
    Owen Jacobson, May 1, 2008
    #2
    1. Advertising

  3. "Rex Mottram" <> wrote in message
    news:...
    > I actually have an incredibly ugly - if I do say so myself - solution in
    > place which involves having web.xml map all servlets to a common
    > "redirector" servlet. The redirector uses reflection to map the URL to a
    > class; as long as naming standards are adhered to this works. But it's
    > really awful and is known to be fragile, and when I saw a demo of
    > annotation-driven deployment I was really looking forward to throwing out
    > the current hack. Apparently it's not soup yet, though.


    What if your redirector class searched for the annotations itself? Oh,
    right, the usual problem of not being able to iterate the classpath to find
    all of the classes.

    OK, a two-part solution:

    1. The user needs to create a text file servletClasses.txt which contains
    the names of all of their servlet classes, one per line, and stick it in
    their jar at the top level.
    2. Your redirector servlet's init(0 method can load the resource
    /servletClasses.txt , load these classes via reflection, and check their
    annotations for deployment information. If classes are missing or ther
    deployment information is missing, incorrect, or inconsistent, you can issue
    helpful and instructive error messages.

    In fact, since Tomcat implements getResources() correctly (many containers
    do not, for some reason), the users can, if they want, deploy as many jars
    containing servlets as they like, with each jar described by its own
    servletClasses.txt file.

    Not ideal, but simpler than messing with web.xml.
    Mike Schilling, May 2, 2008
    #3
  4. Rex Mottram wrote:
    > Mike Schilling wrote:
    >> OK, a two-part solution:
    >>
    >> 1. The user needs to create a text file servletClasses.txt which
    >> contains the names of all of their servlet classes, one per line,
    >> and stick it in their jar at the top level.
    >> 2. Your redirector servlet's init(0 method can load the resource
    >> /servletClasses.txt , load these classes via reflection, and check
    >> their annotations for deployment information. If classes are
    >> missing or ther deployment information is missing, incorrect, or
    >> inconsistent, you can issue helpful and instructive error messages.
    >>
    >> In fact, since Tomcat implements getResources() correctly (many
    >> containers do not, for some reason), the users can, if they want,
    >> deploy as many jars containing servlets as they like, with each jar
    >> described by its own servletClasses.txt file.

    >
    > Interesting idea, I will look into it (though I do not want to be
    > wedded to Tomcat).


    In case I wasn't clear, the general idea will work in any servlet
    container. It's only the multiple independent jars that won't,
    because you'd need getResources() (as opposed to getResource()) to
    find all of the text files.
    Mike Schilling, May 2, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Albretch
    Replies:
    2
    Views:
    923
    William Brogden
    Jul 11, 2003
  2. Fran Cotton

    Servlet URL Mapping Evil!

    Fran Cotton, Jul 25, 2003, in forum: Java
    Replies:
    0
    Views:
    3,347
    Fran Cotton
    Jul 25, 2003
  3. circuit_breaker
    Replies:
    2
    Views:
    1,966
    Jack Jia
    Apr 4, 2004
  4. sods
    Replies:
    5
    Views:
    2,721
  5. Replies:
    4
    Views:
    672
    santax
    Apr 20, 2007
Loading...

Share This Page