002_SpringBoot整合Servlet

本文介绍两种SpringBoot整合Servlet的方法:一是使用@WebServlet注解配合@ServletComponentScan注解;二是通过ServletRegistrationBean进行注册。
摘要由CSDN通过智能技术生成

1. 通过注解扫描完成Servlet组件的注册(SpringBoot整合Servlet方式一)

1.1. 使用maven构建SpringBoot的名叫spring-boot-servlet项目

1.2. pom.xml 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bjbs</groupId>
	<artifactId>spring-boot-servlet</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.13.RELEASE</version>
	</parent>

	<!-- 修改jdk版本 -->
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- springBoot的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>

1.3. 编写FirstServlet.java

package com.bjbs1.controller;

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

/**
 * SpringBoot整合Servlet方式一
 *
 * <servlet> 
 * 	<servlet-name>FirstServlet</servlet-name>
 * 	<servlet-class>com.bjsxt1.servlet.FirstServlet</servlet-class> 
 * </servlet>
 *
 * <servlet-mapping> 
 * 	<servlet-name>FirstServlet</servlet-name>
 * 	<url-pattern>/first</url-pattern> 
 * </servlet-mapping>
 */
@WebServlet(name = "FirstServlet", urlPatterns = "/first")
public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("FirstServlet............");
	}
}

1.4. @WebServlet注解

1.5. 编写App.java启动类 

package com.bjbs1;

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

/**
 * SpringBoot整合Servlet方式一, SpringBoot启动类
 */
@SpringBootApplication
@ServletComponentScan // 在springBoot启动时会扫描@WebServlet, 并将该类实例化
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

1.6. 配置@ServletComponentScan注解, 在SpringBoot启动时会扫描@WebServlet, 并将该类实例化

1.7. 运行项目, 并用浏览器访问FirstServlet 

2. 通过方法完成Servlet组件的注册(SpringBoot整合Servlet方式二)

2.1. 编写SecondServlet.java

package com.bjbs2.controller;

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

/**
 * SpringBoot整合Servlet方式二
 */
public class SecondServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("SecondServlet............");
	}
}

2.2. 编写App2.java启动类

package com.bjbs2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import com.bjbs2.controller.SecondServlet;

/**
 * SpringBoot整合Servlet方式二, SpringBoot启动类
 */
@SpringBootApplication
public class App2 {
	public static void main(String[] args) {
		SpringApplication.run(App2.class, args);
	}

	@Bean
	public ServletRegistrationBean getServletRegistrationBean() {
		ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
		bean.addUrlMappings("/second");
		return bean;
	}
}

2.3. 使用org.springframework.boot.web.servlet.ServletRegistrationBean类注册Servlet

2.4. 运行项目, 并用浏览器访问SecondServlet 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值