java - servlet route with GET, POST, PUT and DELETE -
i'm new java server-side programming, question starting point using servlets (low level without using spring mvc etc.) , build way there, coming node.js background route definition start function (app.get(request, response) {}
, app.post(request, response) {}
etc.), , function receive request
, response
in parameters 1 of http methods (get, post, put, delete).
if can please on starting point of how define methods against route (let's /users
) inside servlet class that'd map http methods while providing request , response in it's parameters.
my attempt
public class firstservlet extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception , ioexception { }
i believe want servlet mappings. can find a bit more info here
but way tell webserver (e.g. tomcat) servlet use answer requests sent given url pattern. map pattern servlet want use serve it.
you can find more info on inner workings here.
edit: if want handle verbs can use service. first link:
you may have seen other servlet examples implement dopost() and/or doget() methods. these methods reply post or requests; if want handle request types single method, servlet can implement service() method. (however, if choose implement service() method, cannot implement dopost() or doget() methods, unless call super.service() @ beginning of service() method.) http servlet specification describes other methods used handle other request types, of these methods collectively referred service methods.
all service methods take same parameter arguments. httpservletrequest provides information request, , servlet uses httpservletresponse reply http client. service method looks following:
public void service(httpservletrequest req, httpservletresponse res) throws ioexception { ... }
Comments
Post a Comment