当前位置: 首页 > news >正文

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");
    }
}

运行结果
在这里插入图片描述

相关文章:

  • 制作一个网站首页/佛山网站优化服务
  • 网站首页的布局方式/seo站长优化工具
  • 南昌网站建设推广专家/软文自助发稿平台oem
  • 公司网站建设管理办法/seo按天计费系统
  • 自己的网站做怎样的优化调整/友情链接平台站长资源
  • 做零食的网站/上海网络seo公司
  • 华为MPLS跨域C2方案实验配置
  • 《Linux Shell脚本攻略》学习笔记-第四章
  • Dns与httpDNS的区别
  • SSL协议未开启是什么意思?
  • leetcode 648. 单词替换【python3哈希集与两种字典树的方法的思考过程整理】
  • [解题报告] CSDN竞赛第24期
  • Ubuntu22.04 设置静态IP
  • Golang网络聊天室案例
  • spring系列 事务
  • Unity 过场工具(Cutscene)设计(二)
  • 蓝桥杯STM32G431RBT6学习——按键
  • 【C语言课程设计】通讯录(1.0版本)