Springboot入门之servlet

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangyuhuangyu/article/details/80693346

springboot中使用servlet开发有两种方式:

    1,注解方式

    2,配置方式


方式一:创建一个servlet并在servlet上用@WebServlet注解指定请求路径

package com.sts.springboot.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 {

    @Override
    protected 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 {
        doGet(req, resp);
    }
}

2,在启动类的main方法中配置一个servlet组件扫描,让springboot扫描servlet

package com.sts.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan(basePackages = "com.sts.springboot.servlet")
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

方式二:

1,创建一个servlet

package com.sts.springboot.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 {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("he   hello word.....");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

2,创建一个servlet配置类,需要加上@Configuration注解标识这是一个配置类

package com.sts.springboot.config;

import com.sts.springboot.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * springboot没有xml@Configuration可以表示一个springxml配置文件
 * 比如application.xml
 *
 *
 * <bean id="heServletRegistrationBean"   class="org.springframework.boot.web.servlet.ServletRegistrationBean.">
 *
 *
 * </bean>
 */
@Configuration
public class ServletConfig {
//配置servlet,指定访问路径
    @Bean
    public ServletRegistrationBean heServletRegistrationBean(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/heServlet");
        return registrationBean;
    }

}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页