Servlet Standard Annotations
The JSR-315 Java Servlet standard defines a set of annotations that can be used to declare and configure servlets, filters, and listeners directly in a Java class. This is an alternative to declaring the same information in the WEB- INF/web.xml file of your Composite Application Framework (CAF) application. Pertinent code is highlighted in bold.
Example: Declare a Servlet
package caf.war.testapp1.test1;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet1
*/
@WebServlet("/TestServlet1")
public class TestServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet1() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO place your code here
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO place your code here
}
}
Example: Declaring a Filter
package caf.war.testapp1.test1;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class TestFilter1
*/
@WebFilter("/TestFilter1")
public class TestFilter1 implements Filter {
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO place your code here
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO place your code here
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO place your code here
// pass the request along the filter chain
chain.doFilter(request, response);
}
}
Example: Declaring a Servlet Context Listener
package caf.war.testapp1.test1;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class
TestServletContextListener1
*/
@WebListener
public class TestServletContextListener1 implements ServletContextListener {
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent event){
// TODO place your code here
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent event){
// TODO place your code here
}
}