Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading a servlet  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we say "load a servlet" what does it actually mean ?

- where exactly does the servlet get loaded - into the memory, or into the container ??
- also what are the methods that get called on the servlet. init method gets called for sure, what about the service method ? does loading a servlet mean that the servlet gets serviced as well ?
- also regarding the the load on start up tag in web.xml. Can we have two servlets with load on startup value as 1 ?
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- where exactly does the servlet get loaded - into the memory, or into the container ??


As per my understanding container loads the servlets into memory.

- also what are the methods that get called on the servlet. init method gets called for sure, what about the service method ? does loading a servlet mean that the servlet gets serviced as well ?



Source: The init, service, and destroy methods are the servlet's lifecycle methods. The init method is called once by the servlet container after the servlet class has been instantiated to indicate to the servlet that it being placed into service. The init method must complete successfully before the servlet can receive any requests. A servlet programmer can override this method to write initialization code that needs to run only once, such as loading a database driver, initializing values, and so on. In other cases, this method is normally left blank.

The service method is then called by the servlet container to allow the servlet to respond to a request. The servlet container passes a javax.servlet.ServletRequest object and a javax.servlet.ServletResponse object. The ServletRequest object contains the client's HTTP request information and the ServletResponse encapsulates the servlet's response. These two objects enable you to write custom code that determines how the servlet services the client request.

The servlet container calls the destroy method before removing a servlet instance from service. This normally happens when the servlet container is shut down or when the servlet container needs some free memory. This method is called only after all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls destroy, it will not call the service method again on this servlet. The destroy method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, and threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.


- also regarding the the load on start up tag in web.xml. Can we have two servlets with load on startup value as 1 ?

Yes, <load-on-startup> tag specifies that the servlet should be loaded automatically when the web application is started. You can define the value in this tag. So multiple servlets can have this tag.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about just reading the Servlet Specification?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, you.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically ... loading means that the servlet class gets loaded into the container for further use ...

The invocation of the init() method means the initialization of the servlet ... and only after the invocation (calling the init()) a servlet gets its actual resources otherwise its just one another class ...

and the service method gets called whenever a request comes to the container for that particular servlet ...

Lucky
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!