[JAVA]SpringBoot整合Servlet
1.通过注解扫描.
2.通过编写一个方法.
1.注解扫描
以前我们在web写select,需要在web.xml配置<servlet></servlet>和<servlet-mapping></servlet-mapping>等信息。那么使用springboot不必了。
AnnotationServlet.java
这里我们设置的路径是/hello
package xyz.xioaxin12.springboot01servlet.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(name="AnnotationServlet",urlPatterns = "/hello") public class AnnotationServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); resp.getWriter().append("这是springboot整合servlet注解的方式!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
SpringBoot01ServletApplication.java
启动类,这里能看到加了@ServletComponentScan注解,它会自动注册带有@WebServlet的注解。
package xyz.xioaxin12.springboot01servlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class SpringBoot01ServletApplication { public static void main(String[] args) { SpringApplication.run(SpringBoot01ServletApplication.class, args); } }
由于我的8080被占用了,我在application.properties修改成8081端口。
server.port=8081
启动访问localhost:8081/hello
2.通过编写一个方法
为了测试明显,不在原类上修改,重写一个servlet与启动类。
DeployServlet.java
去除了注解。
package xyz.xioaxin12.springboot01servlet.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 DeployServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); resp.getWriter().append("这是springboot整合servlet另一种方式!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
SpringBoot01ServletApplication2.java
一定要添加@Bean
package xyz.xioaxin12.springboot01servlet; 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 xyz.xioaxin12.springboot01servlet.servlet.DeployServlet; @SpringBootApplication public class SpringBoot01ServletApplication2 { public static void main(String[] args) { SpringApplication.run(SpringBoot01ServletApplication2.class, args); } @Bean public ServletRegistrationBean getRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new DeployServlet()); bean.addUrlMappings("/hello2"); return bean; } }
启动访问localhost:8081/hello2
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 xiaoxin1218@qq.com
文章标题:[JAVA]SpringBoot整合Servlet
文章字数:477
本文作者:周信
发布时间:2019-10-22, 22:34:48
最后更新:2023-05-03, 10:25:35
原始链接:http://zx21.xyz/2019/10/22/JAVA-SpringBoot整合Servlet/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。