servlet 3.0导入注释包



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

import javax.servlet.annotation.WebServlet; 

这里的servlet、annotation和WebServlet是javax包中的类或接口吗?

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

From JavaEE 5 annotations were supported,用于定义某些部署属性。它们大多与servlet使用的资源有关。但是servlet仍然只能在web.xml中定义。

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

最新更新