SpringBoot-整合Servlet

导入Pom依赖



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
		 
	
	com.example
	SpringBoot-Servlet
	0.0.1-SNAPSHOT
	SpringBoot-Servlet
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


方法一

  •   定义一个自定义Servlet类并继承HttpServlet类
package com.example.demo.servlet;

import java.io.IOException;

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(name = "myServlet",urlPatterns="/my") // name:给当前servlet取名,urlPatterns:拦截的路径 当前是拦截my
public class MyServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("执行了MyServlet的doGet方法");
	}
}
  • 给Spring Boot添加注解@ServletComponentScan :该注解默认扫描当前包及子包
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

import com.example.demo.servlet.MyServlet;

@SpringBootApplication
@ServletComponentScan // 扫描Servlet类默认扫描当前包及子包
public class SpringBootServletApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootServletApplication.class, args);
	}

}

方法二

  • 定义一个自定义Servlet类并继承HttpServlet类
package com.example.demo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("执行了LoginServlet的doGet方法");
	}
}
  • 将  org.springframework.boot.web.servlet.ServletRegistrationBean添加到Spring容器中
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.example.demo.servlet.LoginServlet;
import com.example.demo.servlet.MyServlet;

@SpringBootApplication
public class SpringBootServletApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootServletApplication.class, args);
	}
	@Bean // 将ServletRegistrationBean添加到Spring容器中
	public ServletRegistrationBean getServletRegistrationBean(){
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
		servletRegistrationBean.setServlet(new LoginServlet()); // 添加Servlet子类
		servletRegistrationBean.addUrlMappings("/login"); // 设置拦截请求
		return servletRegistrationBean;
	}
}

测试

   分别发送 http://localhost:8080/my  和  http://localhost:8080/login 请求

SpringBoot-整合Servlet_第1张图片

你可能感兴趣的:(Java,SpringBoot,SpringBoot,Spring,Servlet)