备案网站内容怎么写,怎么做有趣视频网站,建立网站的内容规划,千博企业网站管理系统上一篇我们对如何创建Controller 来响应JSON 以及如何显示数据到页面中#xff0c;已经有了初步的了解。 Web开发使用 Controller 基本上可以完成大部分需求#xff0c;但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。 当使用spring-Boot时#xff0c;…上一篇我们对如何创建Controller 来响应JSON 以及如何显示数据到页面中已经有了初步的了解。 Web开发使用 Controller 基本上可以完成大部分需求但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。 当使用spring-Boot时嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器如HttpSessionListener监听器。 Spring boot 的主 Servlet 为 DispatcherServlet其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet该如何使用SpringBoot来完成呢 在spring boot中添加自己的Servlet有两种方法代码注册Servlet和注解自动注册Filter和Listener也是如此。 一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 也可以通过实现 ServletContextInitializer 接口直接注册。 二、在 SpringBootApplication 上使用ServletComponentScan 注解后Servlet、Filter、Listener 可以直接通过 WebServlet、WebFilter、WebListener 注解自动注册无需其他代码。 通过代码注册Servlet示例代码 SpringBootSampleApplication.Java package org.springboot.sample;import org.springboot.sample.servlet.MyServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.DispatcherServlet; SpringBootApplication public class SpringBootSampleApplication { /** * 使用代码注册Servlet不需要ServletComponentScan注解 * * return * author SHANHY * create 2016年1月6日 */ Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new MyServlet(), /xs/*);// ServletName默认值为首字母小写即myServlet } public static void main(String[] args) { SpringApplication.run(SpringBootSampleApplication.class, args); } } 12345678910111213141516171819202122232425262728291234567891011121314151617181920212223242526272829MyServlet.java package org.springboot.sample.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet * * author 单红宇(365384722) * myblog http://blog.csdn.net/catoop/ * create 2016年1月6日 */ //WebServlet(urlPatterns/xs/*, descriptionServlet的说明) public class MyServlet extends HttpServlet{ private static final long serialVersionUID -8685285401859800066L; Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(doGet()); doPost(req, resp); } Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(doPost()); resp.setContentType(text/html); PrintWriter out resp.getWriter(); out.println(html); out.println(head); out.println(titleHello World/title); out.println(/head); out.println(body); out.println(h1大家好我的名字叫Servlet/h1); out.println(/body); out.println(/html); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445123456789101112131415161718192021222324252627282930313233343536373839404142434445使用注解注册Servlet示例代码 SpringBootSampleApplication.java package org.springboot.sample;import org.springboot.sample.servlet.MyServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.DispatcherServlet; SpringBootApplication ServletComponentScan public class SpringBootSampleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSampleApplication.class, args); } } 1234567891011121314151617181912345678910111213141516171819MyServlet2.java package org.springboot.sample.servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet * * author 单红宇(365384722) * myblog http://blog.csdn.net/catoop/ * create 2016年1月6日 */ WebServlet(urlPatterns/xs/myservlet, descriptionServlet的说明) // 不指定name的情况下name默认值为类全路径即org.springboot.sample.servlet.MyServlet2 public class MyServlet2 extends HttpServlet{ private static final long serialVersionUID -8685285401859800066L; Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(doGet2()); doPost(req, resp); } Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(doPost2()); resp.setContentType(text/html); PrintWriter out resp.getWriter(); out.println(html); out.println(head); out.println(titleHello World/title); out.println(/head); out.println(body); out.println(h1大家好我的名字叫Servlet2/h1); out.println(/body); out.println(/html); } } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454612345678910111213141516171819202122232425262728293031323334353637383940414243444546使用 WebServlet 注解其中可以设置一些属性。 有个问题DispatcherServlet 默认拦截“/”MyServlet 拦截“/xs/*”MyServlet2 拦截“/xs/myservlet”那么在我们访问 http://localhost:8080/xs/myservlet 的时候系统会怎么处理呢如果访问 http://localhost:8080/xs/abc 的时候又是什么结果呢这里就不给大家卖关子了其结果是“匹配的优先级是从精确到模糊复合条件的Servlet并不会都执行” 既然系统DispatcherServlet 默认拦截“/”那么我们是否能做修改呢答案是肯定的我们在SpringBootSampleApplication中添加代码 /*** 修改DispatcherServlet默认配置** param dispatcherServlet* return* author SHANHY* create 2016年1月6日 */ Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration new ServletRegistrationBean(dispatcherServlet); registration.getUrlMappings().clear(); registration.addUrlMappings(*.do); registration.addUrlMappings(*.json); return registration; } 1234567891011121314151612345678910111213141516当然这里可以对DispatcherServlet做很多修改并非只是UrlMappings。 http://blog.csdn.net/catoop/article/details/50501686 转载于:https://www.cnblogs.com/softidea/p/6065394.html