简体   繁体   中英

how jersey process is different from CGI and servlet?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM