Just Annotation

Starting with version 2.0 of Pax-Web it’s possible to use Servlet 3.0 annotations for Servlets in a web application bundle (WAB). As defined in the Servlet spec it’s possible to have servlet definitions and configuration either in a web.xml or in the annotations of the Servlet. In this case the annotation of the servlet does override the configuration found in the web.xml. According to the servlet spec it’s even possible to skip the web.xml completely. That’s the part where Pax-Web 3.0 will kick in. Starting with version 3.0.0.M2 it’s possible to deploy a purely annotated WAB. It just needs a annotated Servlet

package org.ops4j.pax.web.itest.support;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet (value="/test", name="test")
public class AnnotatedTestServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  private boolean initCalled;

  @Override
  public void init(ServletConfig config) throws ServletException {
    this.initCalled = true;
    super.init(config);
  }

  public boolean isInitCalled() {
    return initCalled;
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("TEST OK");
  }
}

and a little manifest entry

Web-ContextPath: /annotatedTest

That’s all it takes. In this case you don’t even need to create a war bundle, a standard osgi jar bundle will do already.

As usual a complete and working sample can be found at the Pax-Web itest submodule:
ServletAnnotatedIntegrationTest.java

Ein Gedanke zu „Just Annotation“

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.


*