Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java servlet annotations

Tags:

java

servlets

Is there any way to use pure Java servlets not spring mvc request mapping to map a URL to a method?

something like:

@GET(/path/of/{id})
like image 310
Paul Avatar asked Oct 25 '25 07:10

Paul


1 Answers

It's also possible with "plain vanilla" servlets (heck, Spring MVC and JAX-RS are also built on top of servlet API), it only requires a little bit more boilerplate.

@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);
        // ...
    }

}

That's all. Thanks to the new Servlet 3.0 @WebServlet annotation, you don't need any web.xml entry.

See also:

  • Our Servlets wiki page
like image 63
BalusC Avatar answered Oct 27 '25 22:10

BalusC