一、简介

@WebServlet注释用于定义web应用程序中的Servlet组件,此注释用于Java类上。在使用@WebServlet注释时,可以不需要web.xml文件。

1、属性

@WebServlet注释的部分属性如下:

  • String name

    Servlet的名称,对应于<servlet-name>

  • String[] urlPatterns

    URL匹配模式,相当于<url-pattern>

  • String[] value

    urlPatterns作用相同,是一种更方便的方法,允许对类进行极其简单的注释。

  • int loadOnStartup

    加载顺序,默认值为-1,对应于<load-on-startup>

  • WebInitParam[] initParams

    Servlet的初始化参数,对应于<init-param>

  • Boolean asyncSupported

    是否支持异步

  • String description

    Servlet的描述

其中:urlPatterns或value属性必须指定,其他属性都是可选的。

2、依赖

pom.xml文件中添加servlet的依赖:

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>4.0.1</version>
</dependency>

二、样例

1、样例一

  • Servlet
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json");
        PrintWriter printWriter = resp.getWriter();
        printWriter.print("{\"message\":\"Hello World!\"}");
        printWriter.close();
	}
}
  • 运行

访问http://localhost:8080/webservlet/hello,输出:

{"message":"Hello World!"}

2、样例二

  • Servlet
@WebServlet(urlPatterns = {"/helloworld", "/greeting"})
public class MultipleURLServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		possess(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		possess(req, resp);
	}

	private void possess(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/plain");
		PrintWriter writer = resp.getWriter();
		writer.print("URL: " + req.getRequestURL());
		writer.close();
	}
	
}
  • 运行

访问http://localhost:8080/webservlet/helloworld,输出:

URL: http://localhost:8080/webservlet/helloworld

访问http://localhost:8080/webservlet/greeting,输出:

URL: http://localhost:8080/webservlet/greeting

3、样例三

  • Servlet
@WebServlet(
	name = "MyWebServlet", 
	urlPatterns = "/myweb",
	loadOnStartup = 1,
	asyncSupported = true,
	initParams = {
		@WebInitParam(name="hello", value="world"),
		@WebInitParam(name="foo", value="bar")
	}
)
public class MyServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig config = getServletConfig();
        resp.setContentType("text/html");
        PrintWriter printWriter = resp.getWriter();
        printWriter.print("<html>");
        printWriter.print("<body>");
        printWriter.print(String.format("<h1>hello %s, foo %s.</h1>", config.getInitParameter("hello"), config.getInitParameter("foo")));
        printWriter.print("</body>");
        printWriter.print("</html>");
        printWriter.close();
	}
}
  • 运行

访问http://localhost:8080/webservlet/myweb,输出:

hello world, foo bar.
参考资料:

@WebServlet Annotation Example

WebServlet (Java(TM) EE 7 Specification APIs)