org.springframework.boot
spring-boot-starter-parent
2.2.4.RELEASE
org.springframework.boot
spring-boot-starter-web
@WebServlet(name = "firstServlet",urlPatterns = "/firstServlet") //urlPatterns:访问路径
public class firstServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进来了firstServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
1.3. 编写启动类
创建springboot启动类
代码:
@SpringBootApplication
//在spring boot启动时会扫描@WebServlet注解,并创建该类的实例
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
注意:在启动类上需要加上@ServletComponentScan注解 意思是:在启动时扫描@WebServlet注解 ,创建Servlet的实例
1.4. 运行启动类 ,在浏览器输入localhost:8080/firstServlet
控制台输出信息
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
//添加一个方法,方法名无要求,必须返回ServletRegistrationBean。注册Servlet对象
@Bean //主键等价于标签
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean=
new ServletRegistrationBean(new SecondServlet(),"/SecondServlet");
return bean;
}
}
@WebFilter(filterName = "firstFilter", urlPatterns = "/firstFilter")
public class firstFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("----进入FirstFilter-----");
chain.doFilter(request, response);//放行
System.out.println("----离开FirstFilter-----");
}
}
@SpringBootApplication
//在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
运行启动类,在浏览器输入 localhost:8080/firstFilter
这里报404是因为没有写放行后的路径;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
//添加一个方法
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean=
new FilterRegistrationBean(new SecondFilter());
bean.addUrlPatterns("*.do","*.jsp","/SecondFilter"); //以.do , .jsp , SecondFilter结尾路径的都会进到过滤器
return bean;
}
}
@WebListener()
public class firstListener implements ServletContextListener{
//监听application对象的创建
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("-----------application对象创建-----------------");
}
}
@SpringBootApplication
@ServletComponentScan //在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
代码一样 省略代码 直接上代码
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
ServletListenerRegistrationBean bean=
new ServletListenerRegistrationBean(new firstListener());
return bean;
}
}
1.在src/main 下创建一个webapp的目录(目录名称必须为webapp)
在webapp下创建不同目录存放不同的静态资源,如:images 放图片 .
2. 运行启动类访问 直接访问资源路径
在src/main/resources下创建一个static的目录(目录名称必须为static)
在static下创建不同目录存放不同的静态资源,如:images 放图片 .
2. 运行启动类访问浏览器 直接访问资源路径
以上就是本教程的相关内容,感谢观看,转载请注明出处