Home > Java > javaTutorial > How to use Servlet in SpringBoot

How to use Servlet in SpringBoot

王林
Release: 2023-05-10 17:22:06
forward
1765 people have browsed it

1. Method 1 (using annotations)

First, we write a Servlet. The requirement is to simply print a sentence.

Use the @WebServlet annotation above the MyServlet class to create a Servlet.

package com.songzihao.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().println("My SpringBoot Servlet-1");
        resp.getWriter().flush();
        resp.getWriter().close();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
Copy after login

Then use the annotation @ServletComponentScan annotation above the entry class of the SpringBoot project to scan the annotations in the Servlet.

package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
 
@SpringBootApplication //开启spring配置
@ServletComponentScan(basePackages = "com.songzihao.springboot.servlet")
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Copy after login

Finally start the test.

How to use Servlet in SpringBoot

#2. Method 2 (define configuration class)

Still write a Servlet first. No annotations are used this time.

package com.songzihao.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 MyServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("My SpringBoot Servlet-2");
        resp.getWriter().flush();
        resp.getWriter().close();
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
Copy after login

Then write a configuration class! ! !

The @Configuration annotation is used above this class. The table name of this class is a configuration class, which is equivalent to various previous xml configuration files.

Use the @Bean annotation above the method in the class. ServletRegistrationBean is equivalent to a Servlet registration class, similar to the previous and tags.

package com.songzihao.springboot.config;
import com.songzihao.springboot.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 *
 */
@Configuration //该注解将此类定义为一个配置类(相当于一个xml配置文件)
public class ServletConfig {
 
    /**
     * @Bean 是一个方法级别上的注解,主要用在配置类里
     * 相当于一个 <beans>
     *              <bean id="..." class="..." />
     *          </beans>
     * @return
     */
    @Bean
    public ServletRegistrationBean myServletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(
                new MyServlet(),"/myservlet"
        );
        return servletRegistrationBean;
    }
}
Copy after login

Finally start the test.

package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Copy after login

How to use Servlet in SpringBoot

The above is the detailed content of How to use Servlet in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template