Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet Filtering using java EE 6 annotation?

Is it possible to simulate a servlet filter chain using @ApplicationPath and @Path annotations in EE 6?

Example:

@ApplicationPath("/api")
class Filter extends Application { 
    @Path("/*")
    public void filter() {
        log.info("Request to API");
    }
}

...

@Path("/foo")
class Foo {
    @GET
    @Path("/bar")
    @Produces("text/plain")
    public String bar() {
        return "Hello World";
    }
}

Where the URL would be http://foobar.com/api/foo/bar but the "filter" method would be invoked as if it were a servlet filter chain. I know the approach above wont work but is there an annotated approach in this ilk that would achieve the same as if the "Filter" was configured from web.xml file?

like image 974
travega Avatar asked Nov 16 '11 04:11

travega


People also ask

What is the use of @WebServlet annotation?

Use the @WebServlet annotation to define a servlet component in a web application. This annotation is specified on a class and contains metadata about the servlet being declared. The annotated servlet must specify at least one URL pattern. This is done by using the urlPatterns or value attribute on the annotation.

What is servlet filter in Java?

Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes − To intercept requests from a client before they access a resource at back end. To manipulate responses from server before they are sent back to the client.

What is filter in Java EE?

Java Servlet Filter is used to intercept the client request and do some pre-processing. It can also intercept the response and do post-processing before sending to the client in web application.

What do you mean by filtering request and response in servlet?

A filter is an object that can transform the header and content (or both) of a request or response. Filters differ from web components in that filters usually do not themselves create a response.


1 Answers

JBoss 7 (even JBoss 6 already) supports Java EE 6 which in turn covers Servlet 3.0. Perhaps your web.xml is incorrectly been declared conform Servlet 2.5 which caused the @WebFilter not to work at all. Ensure that the root declaration of your web.xml is been declared conform Servlet 3.0 like follows:

<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

Then you can just use @WebFilter:

@WebFilter("/api/*")
public class FooFilter implements Filter {

    // ...

}

The examples which you've shown there are by the way part of JAX-RS, which is another API (a RESTful webservice API) built on top of Servlets. To learn more about JAX-RS, the Jersey user guide may be useful.

See also:

  • Our Servlet-Filters wiki page
like image 108
BalusC Avatar answered Sep 22 '22 16:09

BalusC