【问题标题】:Java servlet annotationsJava servlet 注释
【发布时间】:2013-01-30 22:37:56
【问题描述】:

有没有办法使用纯Java servlet而不是spring mvc请求映射来将URL映射到方法?

类似:

@GET(/path/of/{id})

【问题讨论】:

    标签: java servlets


    【解决方案1】:

    “plain vanilla”servlet 也可以实现(见鬼,Spring MVC 和 JAX-RS 也构建在 servlet API 之上),它只需要更多样板。

    @WebServlet("/path/of/*")
    public class PathOfServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String id = request.getPathInfo().substring(1);
            // ...
        }
    
    }
    

    就是这样。感谢新的 Servlet 3.0 @WebServlet 注释,您不需要任何 web.xml 条目。

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2011-12-02
      相关资源
      最近更新 更多