1

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

import javax.servlet.annotation.WebServlet; 

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

4

1 回答 1

8

在注释之前,定义任何部署属性的唯一方法是使用部署描述符。对于 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.
  }

 }
于 2012-04-02T05:21:30.370 回答