SpringBoot 注册自己的Servlet(三种方式)
SpringBoot 注册自己的Servlet(三种方式)
目录
- SpringBoot 注册自己的Servlet(三种方式)
- 方法1:使用servlet3.0规范提供的注解方式注册
- Servlet
- 1,声明servlet及映射
- 2,加上@ServletComponentScan 才会扫描加了这个注解
- 运行结果
- 方法2:使用springboot提供的注册
- Servlet
- 配置类
- 运行结果
- 方法3:动态注册:
- Servlet类
- 配置类
方法1:使用servlet3.0规范提供的注解方式注册
@WebServlet
Servlet
1,声明servlet及映射
com/bjpowernode/springbootservlet01/servlet
MyServlet.java
package com.bjpowernode.springbootservlet01.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name="MyServlet",urlPatterns = "/myServlet")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req,resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("This is my servlet");
}
}
2,加上@ServletComponentScan 才会扫描加了这个注解
运行结果
方法2:使用springboot提供的注册
Servlet
com/bjpowernode/springbootservlet02/servlet
BeanServletTest.java
package com.bjpowernode.springbootservlet02.servlet;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
public class BeanServletTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("hello this is servlet");
}
}
配置类
com/bjpowernode/springbootservlet02/config
MyWebMvcConfigurer.java
package com.bjpowernode.springbootservlet02.config;
import com.bjpowernode.springbootservlet02.servlet.BeanServletTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.ServletRegistration;
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Autowired
private BeanServletTest beanServletTest;
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
// servletRegistrationBean.setServlet(new BeanServletTest());
servletRegistrationBean.setServlet(beanServletTest);
servletRegistrationBean.setName("beanServlet");
servletRegistrationBean.addUrlMappings("/beanServlet");
return servletRegistrationBean;
}
}
运行结果
方法3:动态注册:
Servlet类
package com.bjpowernode.springbootservlet03.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("This is my servlet");
}
}
配置类
package com.bjpowernode.springbootservlet03.config;
import com.bjpowernode.springbootservlet03.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebServlet;
@Component
public class MyServletConfig implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
ServletRegistration.Dynamic myServlet = servletContext.addServlet("MyServlet", MyServlet.class);
myServlet.addMapping("/myServlet");
}
}
运行结果