This discussion is archived
1 Reply Latest reply: Aug 29, 2011 6:56 AM by 885069 RSS

Servlet 3.0 server specifications

885069 Newbie
Currently Being Moderated
Hi, i'm italian and sorry for my bad english.
I'm trying new servlet features expecially the "pluggability".(8.1.4 @WebListener)
In tutorial i can see this example:
@WebListener
public class MyListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
sc.addServlet("myServlet", "Sample servlet",
"foo.bar.MyServlet", null, -1);
sc.addServletMapping("myServlet", new String[] {
"/urlpattern/*" });
}
}
however searching in API, function "addServlet" has two param instead four and "addServletMapping" daesn't exist.
I tryed with addServlet's right parameters and as I imagined, web.xml result corrupted when addServletMapping is called
how solve this problems?
  • 1. Re: Servlet 3.0 server specifications
    885069 Newbie
    Currently Being Moderated
    solved, i found the right code

    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletRegistration;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class WebXmlManager implements ServletContextListener {
         public void contextInitialized(ServletContextEvent arg0) {
              ServletContext context = arg0.getServletContext();
              ServletRegistration.Dynamic dynamic = context.addServlet("test","core.Test");
              dynamic.addMapping("/Test");
         }
    }
    however there isn't any way to run the Programmatic definition of component for each request? i receive this exception : "Servlets can not be added to context /app as the context has been initialised"

    Edited by: Jstar on 29-ago-2011 6.55

    Edited by: Jstar on 29-ago-2011 6.56

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points