Map servlet programmatically instead of using web.xml or annotations

前端 未结 3 743
春和景丽
春和景丽 2021-01-05 17:26

How can I implement this mapping programmatically without web.xml or annotations? The mission is not to use any framework like spring or something else.

<         


        
3条回答
  •  不知归路
    2021-01-05 17:53

    You can use annotations to achieve this using code.

    import java.io.IOException;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/hello")
    public class HelloServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
            response.getWriter().println("Hello");
        }
    }
    

    You can read about annotations here, here and here

提交回复
热议问题