SpringBoot学习笔记11-SpringBoot 中使用 Servlet

可以通过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
运行截图如下:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章