spring boot最新教程(八):在spring boot中使用servlet的两种方式

      Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。当使用Spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。
       Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的

Servlet,该如何使用SpringBoot来完成呢?

方法一:通过ServletRegistrationBean进行代码注册

servlet代码:

package com.obtk.servlets;

import java.io.IOException;
import java.io.PrintWriter;

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

public class MyServlet01 extends HttpServlet{
	private static final long serialVersionUID = 2747923998779663176L;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.println("");
		out.println("");
		out.println("Hello World");
		out.println("");
		out.println("");
		out.println("

这是:MyServlet01

"); out.println(""); out.println(""); out.close(); out.flush(); } }

注册代码

package com.wx.boot;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.obtk.servlets.MyServlet01;

@Configuration
public class ServletConfig {
	@Bean
	public ServletRegistrationBean MyServlet1(){
		return new ServletRegistrationBean(new MyServlet01(),"/servlets/*");
	}
}

浏览器里面访问

spring boot最新教程(八):在spring boot中使用servlet的两种方式_第1张图片

方法二:使用注解注册Servlet

package com.obtk.servlets;

import java.io.IOException;
import java.io.PrintWriter;

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(urlPatterns="/servlet2/*")
public class MyServlet02 extends HttpServlet{
	private static final long serialVersionUID = 2747923998779663176L;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.println("");
		out.println("");
		out.println("Hello World");
		out.println("");
		out.println("");
		out.println("

这是:MyServlet02

"); out.println(""); out.println(""); out.close(); out.flush(); } }

然后把servlet所在的包纳入package扫描即可

注意@ServletComponentScan

package com.wx.boot;

import java.nio.charset.Charset;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.StringHttpMessageConverter;

@Configuration
@ComponentScan(basePackages = "com.wx.controler.*")
@ServletComponentScan(basePackages="com.obtk.servlets")
public class SpringConfig {
	@Bean
	public StringHttpMessageConverter stringHttpMessageConverter() {
		StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		return converter;
	}

	//统一错误处理配置
	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
	    return new EmbeddedServletContainerCustomizer() {
	        public void customize(ConfigurableEmbeddedServletContainer container) {
	            //ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
	            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/Err404.html");
	            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/Err500.html");
	            container.addErrorPages(error404Page,error500Page);
	        }
	    };
	}
}
 
  


你可能感兴趣的:(spring,boot)