0

I am newbie to web services.i am trying to understand jersey implementation of restful. i have some doubt that i googled it but could not get satisfactory answer

As we know following life cycle phase of servlet :

Loading and instantiation: At the time of instantiation servlet container call init(ServletConfig) this method is called only once in whole life cycle of servlet. Key point is that container will create object only once in whole life cycle of servlet.

Service :Once Loading and instantiation is done each request coming to this servlet handle by service method.Now for next request container will not create object of servlet again it create separate thread to handle newly coming request.This is one of the major cause of good performance of servlet over CGI application because (In CGI every time there's client request, HTTP server creates new instance of process to serve this request. This is performance killer).

Destroy : Container calls destroy() method to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed.

Am i right ?

Now comming to second part what i got when we create resource in jersey for each comming request container create seperate instance of resource class as we can see each time constructor is called.

@Path("myresource")
public class MyResource {
     public MyResource(){
     System.out.prinln("hi");
     }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

My question is how jersey process is different from CGI ? if not why people compromise with performance?

Noman Akhtar
  • 690
  • 1
  • 12
  • 17
  • Possible duplicate of [Java CGI vs. Servlets](http://stackoverflow.com/questions/8401465/java-cgi-vs-servlets) – stdunbar Apr 24 '17 at 18:58
  • I know Java CGI vs. Servlets that's why i explained it in question, i want to know how jersey is different from these? – Noman Akhtar Apr 25 '17 at 05:14

1 Answers1

0

Ultimately the JAX-RS specification says (section 3.1.1 of the JAX-RS 2.0 spec)

By default a new resource class instance is created for each request to that resource. First the constructor (see Section 3.1.2) is called, then any requested dependencies are injected (see Section 3.2), then the appropriate method (see Section 3.3) is invoked and finally the object is made available for garbage collection

So yes, there are some similarities between JAX-RS resource classes and a CGI script. But the major difference is that it is significantly cheaper to create a new object and garbage collect it than it is to fork/exec a new process. I would argue that for super high performance JAX-RS could be slower than Servlets. The implementation is up to you to decide what works best.

stdunbar
  • 16,263
  • 11
  • 31
  • 53