SpringBoot学习笔记11-SpringBoot 中使用 Servlet
程序员文章站
2022-07-13 13:37:01
...
可以通过2中方式实现:
方式一,通过注解方式实现;
1、使用Servlet3的注解方式编写一个Servlet
创建servlet包
编写MyServlet
package com.springboot.web.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 {
private static final long serialVersionUID = -4134217146900871026L;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("hello word");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
2、在main方法的主类上添加注解:
@ServletComponentScan(basePackages=“com.springboot.web.servlet”)
package com.springboot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan(basePackages="com.springboot.web.servlet")
public class SpringbootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootWebApplication.class, args);
}
}
测试:
【http://localhost:8080/myServlet】
输出:my servlet , hello word
运行截图如下:
方式二,通过Spring boot的配置类实现;
1、编写一个普通的Servlet(可以看到并没有配置注解)
package com.springboot.web.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 HeServlet extends HttpServlet {
private static final long serialVersionUID = -4134217146900871026L;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("hello word");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
2、编写一个Springboot的配置类;
package com.springboot.web.config;
import com.springboot.web.servlet.HeServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** springboot没有xml , @Configuration可以表示一个spring的配置文件,比如:applicationContext.xml */
@Configuration //一定要加上这个注解,成为Springboot的配置类,不然不会生效
public class ServletConfig {
@Bean //这个注解就将这个ServletRegistrationBean变成一个Spring的bean类。
public ServletRegistrationBean heServletRegistrationBean(){
ServletRegistrationBean registration = new ServletRegistrationBean(new HeServlet(), "/heServlet");
return registration;
}
}
测试:
【http://localhost:8080/heServlet】
输出:he servlet , hello word
运行截图如下:
上一篇: golang读取pdf
推荐阅读
-
Android学习笔记--使用剪切板在Activity中传值示例代码
-
Java学习笔记十七:Java中static使用方法
-
ng2学习笔记之bootstrap中的component使用教程
-
Vue 2.0学习笔记之使用$refs访问Vue中的DOM
-
Python中函数参数设置及使用的学习笔记
-
SpringBoot学习笔记12-SpringBoot 中使用 Filter
-
SpringBoot学习笔记11-SpringBoot 中使用 Servlet
-
【ANTLR学习笔记】5:使用监听器构建翻译程序,在g4文件中定制语法分析过程
-
java中枚举api的使用(学习笔记)
-
SpringBoot学习笔记42——使用log4j2.xml配置日志文件的输出及保留