SpringBoot入门系列篇(六):SpringBoot中使用Servlet

前情提要

web开发使用Controller基本能解决大部分的需求,但是有时候我们也需要使用Servlet,因为相对于拦截和监听来说,有时候原生的还是比较好用的,现在就来简单的在SpringBoot中使用这些特殊类吧


SpringBoot中简单使用Servlet

我们知道,SpringBoot的核心控制器DispatcherServlet会处理所有的请求,所以对于我们自己写的Servlet来说,就需要进行相应的注册来让DispatcherServlet核心控制器知道你写的这个Servlet的作用以及处理的请求urlpattern
对Servlet的注册有两种方式:使用代码注册和使用注解来注册,通过代码注册就需要相应的RegistrationBean获得相应的控制,即ServletRegistrationBean,而通过注解来注册就比较简单了,Servlet就直接通过Servlet3.0提供的@WebServlet(urlPattern,descript)来进行注册就可以了,然后在启动类前面添加@ServletComponentScan注解就可以自动扫描同包下的所有Servelt。下面分别通过这两种注册方式来进行Servlet的注册
代码方式进行注册
1.首先需要编写一个Servlet类:
package org.framework.demo.section1;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 在SpringBoot中通过代码注册的方式来注册Servlet
 * @author chengxi
 */
public class MyServlet extends HttpServlet{

    @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.getWriter().print("<h1>Hello MyServlet Response return you this</h1>");
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
}
然后,创建tomcat启动类,在该类中需要给ServletRegistrationBean获得相应的控制,代码如下:
package org.framework.demo.section1;

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

/**
 * 测试SpringBoot使用Servlet的启动类
 * 在Main程序中注册Servlet
 * @author chengxi
 */
@SpringBootApplication
public class ServletApp {

    @Bean
    public ServletRegistrationBean MyServlet1(){
        return new ServletRegistrationBean(new MyServlet(),"/myserv/*");
    }

    public static void main(String[] args){
        SpringApplication.run(ServletApp.class, args);
    }
}
接下来就可以启动tomcat测试类,然后输入网址:localhost:8080/myserv/index即可输出我们dopost方法处理返回的html数据了


注解进行注册:
首先定义一个Servlet类,代码和前面使用代码注册类似,只是需要添加一个注解进行修饰:@WebServlet(urlPattern, descript):
package org.springframework.demo.section;

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

/**
 * 在SpringBoot中通过注解注册的方式来注册Servlet
 * @author chengxi
 */
@WebServlet(urlPatterns = "/serv/*")
public class MyServlet extends HttpServlet{

    @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.getWriter().print("<h1>Hello MyServlet Response return you this</h1>");
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
}
接着创建一个启动类,在里面使用一个注解@ServletComponentScan,该注解会自动扫描对应包下面的Servlet类,默认是启动类所在的包:
package org.springframework.demo.section;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * 启动类测试
 * @author chengxi
 */
@SpringBootApplication
//该注解会扫描相应的包
@ServletComponentScan
public class Main {

    public static void main(String[] args){

        SpringApplication.run(Main.class, args);
    }
}
然后启动测试类,输入网址:localhost:8080/serv/index即可进入已经编写好的模板html文件中


猜你喜欢

转载自blog.csdn.net/qq_27905183/article/details/79075921