@WebServlet
用在Servlet类上 要与 启动类上扫描注解 @ServletComponentScan
注解,一起使用。
1、创建servlet类
在
2、 @WebServlet``(name = ``"MyServlet"``, value = ``"/myServlet"``)
。其中name = ``"MyServlet"
是类名,value = ``"/myServlet"
相当于,urlPatterns = "/my"
是访问路径。
package com.wzy.springbootweb02.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "MyServlet", value = "/myServlet")//,urlPatterns = "/my"
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("111111");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
3、在启动类上面加上扫描注解 @ServletComponentScan``(basePackages = ``"com.wzy.springbootweb02.servlet"``)
扫描Servlet程序。
package com.wzy.springbootweb02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan(basePackages = "com.wzy.springbootweb02.servlet")
@SpringBootApplication
public class SpringbootWeb02Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootWeb02Application.class, args);
}
}