SpringBoot(3) 自定义servlet

在使用SpringBoot进行WEB开发的时候,使用Controller能够解决很多的需求,但是如果我们要使用servlet,filter,listener,intercepter的话就可以去自定义。

SpringBoot的嵌入式Servlet容器会通过扫描注解的方式去注册servlet,filter,listener,intercepter等所有Servlet规范的监听器。

在SpringBoot中自定义自己的Servlet主要有两种方式:

1.通过注解的方式

首先你的SpringBootApplication要使用@ServletComponentScan进行注解,而且Servlet类所在的包不能与SpringBootApplication同级或在它的上一级,因为扫包是从他的子包开始扫的。你的Servlet要使用@WebServlet进行注解,并设置好它的url的属性(如果是filter等的话就使用@WebFilter、@WebListener等进行注解),然后完成你的Servlet就不用其他的代码了。

2.通过代码去注册

代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 
也可以通过实现 ServletContextInitializer 接口直接注册。

下面我将两种方式实现在了同一个项目中,代码如下

SpringBootServletApplication.java

package com.springboot.study;

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.springboot.study.servlet.MyServlet;

@ServletComponentScan
@SpringBootApplication
public class SpringBootServletApplication {
	
	
	@Bean
    public ServletRegistrationBean servletRegistrationBean() {
		// ServletName默认值为首字母小写,即myServlet
        return new ServletRegistrationBean(new MyServlet(), "/mkdlp/*");
    } 
	
	public static void main(String[] args) {
		SpringApplication.run(SpringBootServletApplication.class, args);
	}
}
MyServlet.java

package com.springboot.study.servlet;

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 MyServlet extends HttpServlet {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		System.out.println("=============>DoGet()!!!!");
		doPost(req, resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		System.out.println("=============>DoPost()!!!!");
        resp.setContentType("text/html");  
        PrintWriter out = resp.getWriter();  
        out.println("<html>");  
        out.println("<head>");  
        out.println("<title>Hello World</title>");  
        out.println("</head>");  
        out.println("<body>");  
        out.println("<h1>Servlet</h1>");  
        out.println("</body>");  
        out.println("</html>"); 
	}
}
MyServlet1.java

package com.springboot.study.servlet;

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="/mkdkp/*",description="这是一个通过注解注册的Servlet")
public class Myservlet1 extends HttpServlet {
	
	
	private static final long serialVersionUID = 1L;
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		System.out.println("=============>DoGet()!!!!");
		doPost(req, resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		System.out.println("=============>DoPost()!!!!");
        resp.setContentType("text/html");  
        PrintWriter out = resp.getWriter();  
        out.println("<html>");  
        out.println("<head>");  
        out.println("<title>Hello World</title>");  
        out.println("</head>");  
        out.println("<body>");  
        out.println("<h1>基于注解的Servlet</h1>");  
        out.println("</body>");  
        out.println("</html>"); 
	}

}

项目的目录结构:


现在将项目跑起来,然后去访问

http://localhost:8080/mkdlp/a

http://localhost:8080/mkdkp/a

就可以看到效果了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值