SpringBoot支持Servlet有两种方式 (文件上传)
第一种方式
1:代码注册通过ServletRegistrationBean获得控制
首先定义一个Servlet
DuckTestServlet
package com.duck.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class DuckTestServlet extends HttpServlet{
private static final long serialVersionUID = -8685285401859800066L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("this is DuckTestServlet");
PrintWriter pw = resp.getWriter();
pw.write("this is DuckTestServlet");
pw.flush();
pw.close();
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}
}
2:
ServletRegistrationBean注册定义的Servlet
package com.duck;
import com.duck.servlet.DuckTestServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(scanBasePackages = {"com.duck"},exclude={})
public class DuckAdminApplication {
public static void main(String[] args) {
SpringApplication.run(DuckAdminApplication.class, args);
}
/**
* 通过代码的形式注册一个Servlet,这种形式不需要添加@ServletComponentScan注解来扫描
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new DuckTestServlet(), "/duckTest/*");
}
}
通过访问路径控制urlmapping,此为/duckTest/*,即可拦截到servlet方法
image.png
第二种方式
通过WebServlet方式
1:定义DuckTest2Servlet
package com.duck.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;
import java.io.PrintWriter;
/**
* @WebServlet注解的Servlet注册需要@ServletComponentScan注解的扫描
*/
@WebServlet(urlPatterns = "/duckTest2/*")
public class DuckTest2Servlet extends HttpServlet{
private static final long serialVersionUID = -8685285401859800067L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("this is DuckTest2Servlet");
PrintWriter pw = resp.getWriter();
pw.write("this is DuckTest2Servlet");
pw.flush();
pw.close();
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
}
}
2:启动类添加@ServletComponentScan
package com.duck;
import com.duck.servlet.DuckTestServlet;
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;
@SpringBootApplication(scanBasePackages = {"com.duck"},exclude={})
//扫描工程中的Servlet
@ServletComponentScan(basePackages = {"com.duck"})
public class DuckAdminApplication {
public static void main(String[] args) {
SpringApplication.run(DuckAdminApplication.class, args);
}
/**
* 通过代码的形式注册一个Servlet,这种形式不需要添加@ServletComponentScan注解来扫描
* @return
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new DuckTestServlet(), "/duckTest/*");
}
}
3:通过定义的urlmapping路径,即可进入servlet方法
image.png
网友评论