Servlet3.0的注解自定义原生Servlet实战

Servlet3.0的注解自定义原生Servlet实战
  讲解:使用 Servlet3.0的注解自定义原生Servlet和Listener
    自定义原生Servlet

 1 package net.xdclass.demo.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 @WebServlet(name = "userServlet",urlPatterns = "/v1/api/test/customs")
12 public class UserServlet extends HttpServlet{
13 
14      @Override
15      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
16          
17          resp.getWriter().print("custom sevlet");
18          resp.getWriter().flush();
19          resp.getWriter().close();
20      }
21 
22      
23     @Override
24     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
25             throws ServletException, IOException {
26         this.doGet(req, resp);
27     }
28 
29      
30     
31 }

浏览器输入:http://localhost:8083/v1/api/test/customs

结果:

注意:启动类中要加入@ServletComponentScan注解

posted @ 2019-01-10 16:10  清晨的第一抹阳光  阅读(425)  评论(0编辑  收藏  举报