@Servlet annotation

You must pass arguments to the @Servlet annotation to specify the URLs that your servlet handles. You provide a search pattern to test against the servlet query path. The servlet query path is every character after the computer name, the port, the web application name, and the word "/service". The servlet query path must begin with a slash (/) character. Make sure that you do not create ambiguous servlet query paths by using patterns that match the same strings.

Multiple signatures of the annotation constructor support different use cases.

  • @Servlet(pathPattern : String)

    Use this annotation constructor syntax for straightforward pattern matching, with less flexibility than full regular expressions.

    This annotation constructor declares the servlet URL pattern matching to use Apache org.apache.commons.io.FilenameUtils wildcard syntax. Apache FilenameUtils syntax supports Unix and Windows file names with limited wild card support for ? (single character match) and * (multiple character match) similar to DOS file name syntax. The syntax also supports the Unix meaning of the ~ symbol. Matches are always case sensitive.

  • @Servlet(pathMatcher : block( path : String ) : boolean)

    Use this annotation constructor syntax for highly flexible pattern matching.

    You provide a Gosu block that can do arbitrary matching on the servlet query path. The Gosu block takes a String parameter, which is the servlet query path. Write the block to return true if and only if the String matches the URLs that your servlet handles. You can use methods on the String parameter to perform simple or more complex pattern matching.

    For example, you can use the startsWith method of the String argument. The following example servlet responds to URLs that start with the text "/test" in the servlet query path:

    @Servlet(\ path : String -> path.startsWith("/test"))

    This example servlet responds to any of the following URLs:

    http://localhost:8180/pc/service/test
    http://localhost:8180/pc/service/tester
    http://localhost:8180/pc/service/test/more

    You can use other methods to do more flexible pattern matching, such as full regular expressions. Test your regular expressions thoroughly. For some patterns, not all matching values are valid servlet query paths. The following example interprets regular expressions by using the matches method of the String argument:

    @Servlet(\ path : String -> path.matches("/test([0-9](/.*)?)?"))

    This example servlet responds to URLs that start with the text "/test" in the servlet query path, and optionally a digit and a slash followed by other text:

    • A single page URL:
      http://localhost:8180/pc/service/test
      http://localhost:8180/pc/service/test0
    • An entire virtual file hierarchy, such as:
      http://localhost:8180/pc/service/test1/another/level

    To match multiple page URLs that are not described in traditional hierarchies as a single root directory with subdirectories, you could intercept URLs with the regular expression:

    @Servlet(\ path : String -> path.matches("(/.*)?/subfolder_one_level_down")

    That pattern matches all of the following URLs:

    http://localhost:8180/pc/service/test1/subfolder_one_level_down
    http://localhost:8180/pc/service/test2/subfolder_one_level_down
    http://localhost:8180/pc/service/test3/subfolder_one_level_down

See also