webServlet的传统配置方式
作者:互联网
一、Servlet的传统配置方式
web.xml中的servlet配置
发一个Servlet,都要在web.xml中配置Servlet才能够使用,就比较麻烦。所以Servlet3.0之后提供了注解(annotation)
二、使用注解方式配置Servlet
@WebServlet注解用于标注在一个继承了HttpServlet类之上,属于类级别的注解。
用法形如:
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet{
//处理 GET 方法请求的方法
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
//处理POST方法请求的方法
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}
该注解的作用等价于 在web.xml中配置的该servlet的<servlet-mapping>元素中<url-pattern>的配置
三、@WebServlet注解属性
- /*或者/:拦截所
- *.do:拦截指定后缀
- /user/test:拦截路径
- /user/*.do、/*.do、test*.do都是非法的,启动时候会报错
标签:do,配置,webServlet,user,注解,Servlet,传统,加载 来源: https://www.cnblogs.com/nbfangzhu/p/15575669.html