(1)urlPatterns几种写法
- 几种写法
- @WebServlet(urlPatterns = “/demo”)
- @WebServlet(value = “/demo”)
- @WebServlet(“/demo”) ``` package com.code.web;
import javax.servlet.*; import javax.servlet.annotation.WebServlet; import java.io.IOException;
@WebServlet(value = “/demo”) public class HelloServlet implements Servlet {
/**
* 被创建的时候,调用1次
* @param servletConfig
* @throws ServletException
*/
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init");
}
/**
* 获取ServletConfig配置对象
*/
@Override
public ServletConfig getServletConfig() {
System.out.println("getServletConfig");
return null;
}
/**
* 调用的时候,多次调用多次执行
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service");
}
@Override
public String getServletInfo() {
System.out.println("getServletInfo");
return null;
}
/**
* 销毁的时候,执行1次
*/
@Override
public void destroy() {
System.out.println("destroy");
}
}
- WebServlet注解的几种
public @interface WebServlet { String name() default “”;
String[] value() default {};
String[] urlPatterns() default {};
int loadOnStartup() default -1;
WebInitParam[] initParams() default {};
boolean asyncSupported() default false;
String smallIcon() default "";
String largeIcon() default "";
String description() default "";
String displayName() default "";
}
- urlPatterns是数组,一个Servlet可以定义多个访问路径
@WebServlet({“/d1”,”/d2”}) public class Servlet2 extends GenericServlet
- 其他写法
//多级目录 @WebServlet(“/d1/d1”) // 通配符 @WebServlet(“/d1/“) // xx.action 不能加/ @WebServlet(“.action”) ```