servlet 3.0 import package of annotation

独自空忆成欢 提交于 2019-12-01 12:48:45
Ramesh PVK

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.
  }

 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!