@WebServlet简介
@WebServlet标注允许servlet映射声明在servlet代码,作用同通过映射部署描述符(web.xml)文件的servlet.
简单示例:
package com.test.z;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello, World!</h1>");
out.println("</body></html>");
out.close();
}
}
类顶部的@WebServlet标注声明以com.test.z.HelloWorld为名的servlet URL映射路径为http://<server>