Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapping servlet to serve my requests

Tags:

servlets

I would like to map a servlet to serve the requests that includes 'app' and ends with *.html in the following way

<url-pattern>/app/*.html</url-pattern>

but on running the application it gives me an error

java.lang.IllegalArgumentException: Invalid <url-pattern> 
/app/*.html in servlet mapping

please help me to map that. And please provide me the links where I can learn about these url mapping rule and conventions.

like image 884
Pokuri Avatar asked Mar 11 '11 17:03

Pokuri


1 Answers

This is indeed invalid. The wildcard has to be the first or the last character to indicate a suffix or prefix pattern respectively.

<url-pattern>*.html</url-pattern>

or

<url-pattern>/app/*</url-pattern>

This is all clearly specified in Section 12.2 of Servlet API specification. Here's an extract of relevance:

12.2 Specification of Mappings

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
  • A string beginning with a ‘*.’ prefix is used as an extension mapping.
  • The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<contextroot>/. In this case the path info is ’/’ and the servlet path and context path is empty string (““).
  • A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  • All other strings are used for exact matches only.

To fix this, you have 2 options:

  1. Use the /app/* pattern and do not put non-HTML files in /app. Put them elsewhere.

  2. Use a different prefix pattern like /controller/* and create a Filter which is mapped on /app/* and does the following in doFilter() method:

    String uri = ((HttpServletRequest) request).getRequestURI();
    if (uri.endsWith(".html")) {
        request.getRequestDispatcher("/controller" + uri).forward(request, response);
    } else {
        chain.doFilter(request, response);
    }
    

Related:

  • Design patterns in Java web applications - contains simple MVC example
like image 51
BalusC Avatar answered Sep 22 '22 08:09

BalusC