servlet 3.0 import package of annotation

后端 未结 1 487
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 12:51

In Servlets 3.0 we have to import the annotations package. So i want to know what are classes and interfaces?

import javax.servlet.annotation.WebServlet; 


        
相关标签:
1条回答
  • 2021-01-16 13:24

    Before annotations the only way to define any deployment properties was using deployments descriptors. For Web Applications, it was web.xml.

    From JavaEE 5 annotations were supported which lets you define certain deployment properties. They were mostly related to resources the servlets used. But still the servlets has to defined in web.xml only.

    Starting with Java EE 6, annotations such as @WebServlet, @WebFilter, @WebListener were introduced which lets you define the deployment properties in the java class itself. You do not have to mention them in web.xml. All the properties you can mention in web.xml can now be provided using @WebSerlvet annotation. And one can still override the properties using web.xml tag.

    This is how Servlets can be defined using annotation:

    import javax.servlet.annotation.WebServlet; 
    
     @WebServlet(asyncSupported = false, name = "HelloWorldServlet",
      urlPatterns = {"/hello"}, 
      initParams = {@WebInitParam(name="param1", value="value1"),
                    @WebInitParam(name="param2", value="value2")}
     )
     public HelloWorldServlet extends HttpServlet
     {
    
    
      public void doGet(HttpSerlvetRequest request, HttpServletResponse response)
      {
       //write hello world.
      }
    
     }
    
    0 讨论(0)
提交回复
热议问题