servlet 3.0 import package of annotation

纵然是瞬间 提交于 2019-12-01 10:26:35

问题


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; 

What is here servlet, annotation and WebServlet a class or interface in the javax package?


回答1:


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

 }


来源:https://stackoverflow.com/questions/9967062/servlet-3-0-import-package-of-annotation

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