0

How do you manage specific HTTP method types for Java? I think has to do with servlets. I tried searching online but I don't quite understand it.

I read something to do with extending to the genericServlet which allows implementation of HTTP requests. Then this allows me to utilize HTTP method types? I'm just really confused

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
John McKenzie
  • 420
  • 6
  • 16

2 Answers2

3

You should extend HttpServlet and implement doGet(), doPost(), doPut() etc instead of GenericServlet.

Please read good tutorial on Servlets/JSP.

Look at HttpServlet.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
brb tea
  • 345
  • 1
  • 12
2

HttpServlet class provides generic methods to accomplish this:

  • doGet for GET requests
  • doPost for POST requests
  • doPut for PUT requests
  • doDelete for DELETE requests

And that's all.

If you want to support other HTTP methods, like TRACE or CONNECT, then you should extend from GenericServlet and do all this work manually by overriding GenericServlet#service method. Take into account that this may involve several work. You could also forget about extending from one of these classes and do it all yourself by implementing Servlet interface. Examples of these:

  • DispatchServlet from Spring MVC framework, which extends from HttpServlet.
  • FacesServlet from JavaServer Faces framework, which directly implements Servlet interface and do all the work by itself. It provides support for OPTIONS, HEAD, TRACE and CONNECT methods apart from the 4 methods described above.

If you're specifically looking about how to implement a RESTful API, then it would be better to use a framework that implements JAX-RS like Jersey or RestEasy or Restlet

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332