With the advent pf 3.0 drastic change has come while working with servlets . J2EE 6 has many changes .
Few imp changes are
- No need of having 1 big web.xml .instaed multiple web-fragment.xml can be there
- Dynamic registration of servlets , filters
- Use of annotations
Here i am putting few important annotations provided by specs servlet 3.0 .
@WebServlet
(asyncSupported = false, name = "HelloServlet", urlPatterns = {"/hello"},
initParams = {@WebInitParam(name="param1", value="value1"),
@WebInitParam(name="param2", value="value2")}
)
public class TestServlet extends javax.servlet.http.HttpServlet {
....
}
----------------------------------------------------------
@WebFilter
(urlPatterns={"/myurl"},
initParams={ @InitParam(name="mesg", value="my filter") })
public class TestFilter implements javax.servlet.Filter {
....
public void init(FilterConfig filterConfig) throws ServletException {
....
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
....
}
public void destroy() {
....
}
}
---------------------------------------------------------
@WebServletContextListener
public class TestServletContextListener implements javax.servlet.ServletContextListener {
....
public void contextInitialized(ServletContextEvent sce) {
....
}
public void contextDestroyed(ServletContextEvent sce) {
....
}
}
---------------------------------------------------------
@MultipartConfig(location="c:\\tmp", fileSizeThreshold=1024*1024,
maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
Above annotation can be used along with @WebServlet
For ordering of Filter / Servlets < absolute - ordering > tag
should be configured in web.xml