这篇文章主要介绍了SpringBoot中使用Servlet的两种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
1.方式一(使用注解)
首先,我们写一个Servlet。要求就是简单的打印一句话。
在MyServlet这个类的上方使用 @WebServlet 注解来创建Servlet即可。
package com.songzihao.springboot.servlet;
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;
/**
*
*/
@WebServlet(urlPatterns = "/myservlet")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("My SpringBoot Servlet-1");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
之后在SpringBoot项目的入口类上方使用注解 @ServletComponentScan 注解来扫描Servlet中的注解即可。
package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication //开启spring配置
@ServletComponentScan(basePackages = "com.songzihao.springboot.servlet")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
最后启动测试。
2.方式二(定义配置类)
仍然是先写一个 Servlet。这次不使用注解。
package com.songzihao.springboot.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*
*/
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("My SpringBoot Servlet-2");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
然后再写一个配置类!!!
这个类的上方使用 @Configuration 注解,表名该类是一个配置类,相当于之前的各种xml配置文件。
在类中的方法上方使用 @Bean 注解,ServletRegistrationBean 这相当于是一个Servlet注册类,类似于之前的 <servlet>、<servlet-mapping> 标签的作用。
package com.songzihao.springboot.config;
import com.songzihao.springboot.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
*/
@Configuration //该注解将此类定义为一个配置类(相当于一个xml配置文件)
public class ServletConfig {
/**
* @Bean 是一个方法级别上的注解,主要用在配置类里
* 相当于一个 <beans>
* <bean id="..." class="..." />
* </beans>
* @return
*/
@Bean
public ServletRegistrationBean myServletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(
new MyServlet(),"/myservlet"
);
return servletRegistrationBean;
}
}
最后启动测试。
package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:SpringBoot中使用Servlet的两种方式小结


猜你喜欢
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- Java实现顺序表的操作详解 2023-05-19
- 深入了解Spring的事务传播机制 2023-06-02
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- Java中的日期时间处理及格式化处理 2023-04-18
- JSP页面间传值问题实例简析 2023-08-03
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- JSP 制作验证码的实例详解 2023-07-30
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17