servlet 3.0导入包的注释

use*_*920 1 servlets servlet-3.0

在Servlets 3.0中,我们必须导入注释包.所以我想知道什么是类和接口?

import javax.servlet.annotation.WebServlet; 
Run Code Online (Sandbox Code Playgroud)

这里的servlet,annotation和WebServlet是javax包中的类或接口是什么?

Ram*_*PVK 6

在注释之前,定义任何部署属性的唯一方法是使用部署描述符.对于Web应用程序,它是web.xml.

From JavaEE 5 annotations were supported它允许您定义某些部署属性.它们主要与servlet使用的资源相关.但是仍然只能在web.xml中定义servlet.

Starting with Java EE 6, annotations such as @WebServlet, @WebFilter, @WebListener were introduced它允许您在java类本身中定义部署属性.您不必在web.xml中提及它们.All the properties you can mention in web.xml can now be provided using @WebSerlvet annotation.还可以使用web.xml标记覆盖属性.

这就是使用注释定义Servlet的方法:

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

 }
Run Code Online (Sandbox Code Playgroud)