How to use annotations instead of web.xml in the servlet to specify url

后端 未结 2 1900
慢半拍i
慢半拍i 2020-12-11 05:53

How to provide an annotation mapping for web.XML in annotation. I have done with web.XML. I want to try with Annotation mapping, like so:

 
           


        
2条回答
  •  隐瞒了意图╮
    2020-12-11 06:21

    A simple example is :

    @WebServlet(value="/hello")
    public class HelloServlet extends HttpServlet {
    
        @Override
        public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
    
        // then write the data of the response
        String username = request.getParameter("username");
        if (username != null && username.length() > 0) {
            out.println("

    Hello, " + username + "!

    "); } } }

提交回复
热议问题