开发者

How do servlets work?

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 ht开发者_如何学JAVAml 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.


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜