java - Use of the Service method in Servlet while there is already doGet and other 6 methods present -


i coding simple servlet . prints values passed user , or in respective url parameters. when implementing doget method , saw same function can performed service method . code below :

protected void service(httpservletrequest req, httpservletresponse resp)         throws servletexception, ioexception {       printwriter o = resp.getwriter();     string user = req.getparameter("username");      httpsession session = req.getsession();     servletcontext context = req.getservletcontext();      if (user != "" && user != null) {          session.setattribute("myhuzu", user);         context.setattribute("myhuzu", user);     }      o.println("request parameter has value " + user);     o.println("session parameter  has user name "             + session.getattribute("myhuzu"));      o.println("context parameter  has user name "             + context.getattribute("myhuzu"));      o.println("init parameter  has user name "             + getservletconfig().getinitparameter("default"));   } 

}

so question , why need doget or dopost methods , when service method taking care of thing.as above code runs perfect , runs perfect if kept in doget method , why din't kept 1 out of them ? note : know servlet life cycle , other related concepts , please don't explain concepts.

the service method in httpservlet class check method in head request , redirect specified method when request doget method called, used when servlet answer diferrent purpose diferent methods, in rest service, when have request, you'll return information, when have put request, you'll update information, servlet provide method you.

plus guarantee servlet won't answer wrong request, using service method did, can call servlet strange request "request method test" , servlet responde, , keep code cleaner.

see original service code:

protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         string method = req.getmethod();         long errmsg;         if(method.equals("get")) {             errmsg = this.getlastmodified(req);             if(errmsg == -1l) {                 this.doget(req, resp);             } else {                 long ifmodifiedsince = req.getdateheader("if-modified-since");                 if(ifmodifiedsince < errmsg) {                     this.maybesetlastmodified(resp, errmsg);                     this.doget(req, resp);                 } else {                     resp.setstatus(304);                 }             }         } else if(method.equals("head")) {             errmsg = this.getlastmodified(req);             this.maybesetlastmodified(resp, errmsg);             this.dohead(req, resp);         } else if(method.equals("post")) {             this.dopost(req, resp);         } else if(method.equals("put")) {             this.doput(req, resp);         } else if(method.equals("delete")) {             this.dodelete(req, resp);         } else if(method.equals("options")) {             this.dooptions(req, resp);         } else if(method.equals("trace")) {             this.dotrace(req, resp);         } else {             string errmsg1 = lstrings.getstring("http.method_not_implemented");             object[] errargs = new object[]{method};             errmsg1 = messageformat.format(errmsg1, errargs);             resp.senderror(501, errmsg1);         }      } 

it reacts diferrent based on request method used.

if custom http request, can set method want, instead of or put or delete send test , service throw "method not implementation" exception, if override service method simple answer, servlet code executed anyway.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -