Spring Boot (教程七: servlet)

来源:互联网 发布:淘宝店铺淘金币签到 编辑:程序博客网 时间:2024/06/07 02:12

GitHub 地址:

https://github.com/asd821300801/Spring-Boot.git


Spring Boot 中使用Servlet


通过代码注册Servlet


需要在SpringbootHelloApplication里注册Servlet

@Bean    public ServletRegistrationBean servletRegistrationBean() {        return new ServletRegistrationBean(new Servlet1(),"/servlet/*");// ServletName默认值为首字母小写,即servlet    }


完整代码


  • SpringbootHelloApplication.java
package com;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import com.example.servlet.Servlet1;/** * Spring Boot 教程一 * Spring Boot 入门 * @author LingDu */@SpringBootApplicationpublic class SpringbootHelloApplication {    @Bean    public ServletRegistrationBean servletRegistrationBean() {        return new ServletRegistrationBean(new Servlet1(),"/servlet/*");// ServletName默认值为首字母小写,即servlet    }    public static void main(String[] args) {        SpringApplication.run(SpringbootHelloApplication.class, args);    }}


在src下创建com.example.servlet包




  • Servlet1.java
package com.example.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** *  通过代码注册Servlet *  *  1、创建Servlet1.java 继承 HttpServlet 实现 doPost() , doGet()方法 *  2、在SpringbootHelloApplication.java文件中添加 *  *  @Bean *  public ServletRegistrationBean servletRegistrationBean() { *     return new ServletRegistrationBean(new Servlet1(),"/servlet/*");// ServletName默认值为首字母小写,即servlet *  } *  *  * @author LingDu */public class Servlet1 extends HttpServlet{    /**     *      */    private static final long serialVersionUID = 1L;    @Override    public void init() throws ServletException {        System.out.println("Servlet初始化");        super.init();    }    @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");          String id = req.getParameter("id");        if(id != null && !id.equals("")){            resp.getWriter().append("-----------> doPost()  id=" + id);            return;        }        resp.getWriter().append("-----------> doPost()");    }}

http://localhost:8080/servlet/servlet?id=1

1

http://localhost:8080/servlet/

2

3



使用注解的方式注册servlet


使用注解方式注册Servlet需要在SpringbootHelloApplication里添加@ServletComponentScan注解



完整代码

  • SpringbootHelloApplication .java
package com;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;/** * Spring Boot 教程一 * Spring Boot 入门 * @author LingDu */@SpringBootApplication@ServletComponentScan //使用注解的方式注册servlet需要在SpringbootHelloApplication.java中添加@ServletComponentScan注解public class SpringbootHelloApplication {       public static void main(String[] args) {        SpringApplication.run(SpringbootHelloApplication.class, args);    }}

  • servlet2.java
package com.example.servlet;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;/** * 使用注解注册Servlet *  *  *  * @author LingDu *///不指定name的情况下,name默认值为类全路径,即org.springboot.sample.servlet.MyServlet2@WebServlet(urlPatterns="/servlet/servlet2", description="Servlet的说明") public class Servlet2 extends HttpServlet {    /**     *      */    private static final long serialVersionUID = 1L;    @Override    public void init() throws ServletException {        System.out.println("Servlet初始化");        super.init();    }    @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");          String id = req.getParameter("id");        if(id != null && !id.equals("")){            resp.getWriter().append("-----------> doPost()  id=" + id);            return;        }        resp.getWriter().append("-----------> doPost()");    }}

http://localhost:8080/servlet/servlet2?id=1

4


修改DispatcherServlet默认配置


在SpringbootHelloApplication .java中添加ServletRegistrationBean

/**     * 修改DispatcherServlet默认配置     *     * @param dispatcherServlet     * @author LingDu     */    @Bean    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);        registration.getUrlMappings().clear();        registration.addUrlMappings("*.action"); //只有*.action 的请求能通过        registration.addUrlMappings("*.json");        return registration;    }
@WebServlet访问路径添加上.action@WebServlet(urlPatterns="/servlet/servlet2.action", description="通过注解注册") 


访问:

http://localhost:8080/servlet/servlet2.action

5



项目工程图

6

原创粉丝点击