tags:

views:

347

answers:

5

I write couples of servlet program, however I dont think I fully understand how servlet run. So here is couples question that I have:

All the code I wrote about Servlet only run on Netbeans with apache tomcat run behind. When I run my html file that make request to the servlet, it usually give error saying that it could not find the servlet, I then have to redeploy the servlet then everything would run fine. Seem like my servlet timeout after a while or something.

Does the servlet run all the time? Servlet has init() and destroy(), so I guess it wont run all the time. So then when does it start and when does it end? Does it start when there is a request from the client, and end when it is time out? And how does I fixed my problem that I have to constantly redeploy the servlet. Thank you very much.

+2  A: 

Under normal circumstances, a servlet is only destroyed at shutdown (ie when the application container, such as Tomcat, is shut down). Otherwise it remains in memory for the duration of the application. I couldn't say what's going on with your Netbeans setup, but try deploying your WAR file to a standalone Tomcat installation and see if the problem doesn't go away.

Another time that the application container will call destroy on a servlet is if it is running out of memory, but this is far less common.

Regarding your question about requests, a servlet is designed to handle many requests. It is said that the servlet is application-scoped, whereas the request has its own scope.

danben
when u say deploy the WAR file to a standalone Tomcat, it just mean to the WAR file in the webapps folder in Apache Tomcat folder. Am I correct? Do u have to do anything else to actually deploy it, or Tomcat will automatically deploy the WAR file for you
Harry Pham
No, Tomcat will automatically expand the WAR file. You will know this has happened when you see a directory under `webapps` with the same name as the WAR file (for example, if you had deployed `myapp.war`, you would see a directory `$CATALINA_HOME/webapps/myapp/). Depending on the version of Tomcat you are using, you may need to restart Tomcat, or it may happen automatically.
danben
A: 

you need to research the servlet lifecycle - that's what the init() and destroy methods are there for

normally init() is called once, when the serlvet is first called (unless you did something like set it to autorun in tomcat)

and destroy() is called when the container shuts down

dopost() or doGet() (if it's an HTTP servlet) are called for each request

phatmanace
+1  A: 

Please see this sample chapter for a more in-depth look at the Servlet lifecycle. Explaining all of the nuances of this lifecycle is beyond the scope of StackOverflow, IMO.

MetroidFan2002
+1  A: 

A servlet "runs" only when it's invoked. The server will wait for a connection to come from the client, read the headers, find the proper servlet based on the mappings in web.xml, and then call the service() method of that servlet. The servlet object, will remain in-memory until the container decides to dispose it (which it may do at any time that it's not servicing requests). If the server decides to dispose of a particular servlet instance, it will create a new one the next time a request comes in for the servlet.

Which means you should not be getting an error that says the server can't find your servlet. Assuming that the application has been deployed, and there is a correct servlet mapping, the container will be able to process the request. If you edit your request and paste the exact error message, someone may be able to tell you why this isn't happening.

kdgregory
Thank you. Your response help me a lot. It is not often that my servlet get shut down. When It will, I will try to post the exact error. For now, I will try to deploy WAR file onto a standalone TOMCAT like other suggested.
Harry Pham
A: 

You're apparently in middle of developing with servlets. You need to ensure that the webapp is fully published whenever you have made changes to web.xml or any of the Servlet classes. Otherwise you may risk that the resource cannot be found.

In easy terms, a "resource not found" error is basically exactly the same as a "404 page not found". The servlet container can't seem to find a resource which matches the URL or url-pattern. That's all.

As to the Servlet lifecycle, it will be created only once during webapp startup (publish, create of context), the init() method will be called and the instance will be kept in the server's memory in a sort of a Map<Url-Pattern, Servlet>. If you have overriden the init() method in your Servlet, then it will be called. The servlet container will do the same for all of the servlets declared in web.xml (or as per Java EE 6, annotated with @WebServlet).

Everytime a request whose URL matches the url-pattern of the Servlet, the (inherited) service() method will be invoked. The normal HttpServlet implementation will then determine the method to be executed based on HttpServletRequest#getMethod(). If you did override any of those methods (doGet(), doPost(), etc) in your Servlet, then it will be invoked accordingly.

Finally, when the webapp is about to shutdown (unpublish, destroy of context), then the destroy() will be invoked for any of the Servlet instances kept in server's memory. If you have overriden the destroy() method in your Servlet, then it will be called.

BalusC
when u said, "ensure that the webapp is fully published", does it just mean copy the updated WAR file to the webapp folder in Tomcat?
Harry Pham
I don't do Netbeans, but in Eclipse you would see the status "synchronized" in the server console. See if something similar exist in Netbeans.
BalusC