Servlet3.0或4.0下@WebServlet注解配置Servlet

@WebServlet常用属性
Servlet3.0或4.0下@WebServlet注解配置Servlet_第1张图片
例子:

@WebServlet(name = "FilterServlet", urlPatterns = "/FilterServlet",
initParams = {@WebInitParam(name = "password",value = "123"),
			  @WebInitParam(name = "name",value = "value")}
			)
public class FilterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过getServletConfig();获得servletConfig对象;
        //可以通过servletConfig对象获得初始化参数等信息
        ServletConfig servletConfig = getServletConfig();
        String password = servletConfig.getInitParameter("password");
        String name= servletConfig.getInitParameter("name");
        System.out.println(password);//输出123
        System.out.println(name);//输出value
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

你可能感兴趣的:(Servlet3.0或4.0下@WebServlet注解配置Servlet)