19:50
Life Cycle Of Servlet
Web container maintains the life cycle of the Servlet.
1-Servlet classes are loaded
2-init method is invoked.
3-service method is invoked.
4-destroy method is invoked.
Life Cycle in Detail:-
1-When a server loads a servlet, it runs the servlet's init method. Even though most servlets are run in multi-threaded servers, there are no concurrency issues during servlet initialization. This is because the server calls the init method once, when it loads the servlet, and will not call it again unless it is reloading the servlet. The server can not reload a servlet until after it has removed the servlet by calling the destroy method. Initialization is allowed to complete before client requests are handled (that is, before the service method is called) or the servlet is destroyed.
2-After the server loads and initializes the servlet, the servlet is able to handle client requests. It processes them in its service method. Each client's request has its call to the service method run in its own servlet thread: the method receives the client's request, and sends the client its response.
3-Servlets can run multiple service methods at a time. It is important, therefore, that service methods be written in a thread-safe manner. If, for some reason, a server should not run multiple service methods concurrently, the servlet should implement the SingleThreadModel interface. This interface guarantees that no two threads will execute the servlet's service methods concurrently.
4-Servlets run until they are removed from the service. When a server removes a servlet, it runs the servlet's destroy method. The method is run once; the server will not run it again until after it reloads and reinitializes the servlet.
5-When the destroy method runs, however, other threads might be running service requests. If, in cleaning up, it is necessary to access shared resources, that access should be synchronized. During a servlet's lifecycle, it is important to write thread-safe code for destroying the servlet and, unless the servlet implements the SingleThreadModel interface, servicing client requests.
Steps to create Servlet Without Using any IDE:-
1- First of All Create the project folder structure as given below in Image.
Here, we can put any name (project name) in place of web-app(context root).
2-Now , Create servlet class and keep inside the Classes folder.Here created SampleHello.java as Below:-
3-
Now ,Create a welcome file ,Here Created index.jsp as below:-
4- Now Configure web.xml ,as given below:-
5- Now ,compile the servlet class what you have created here is SampleHello.java
using command prompt for compilation of class as below:-
javac –d “dir_of_servletclass_if_not_in_same_dir” –cp “path_of_jar; You_can_add_another_jar_also_after_putting_semicolon” classname.java
since,Here I am in same directory of servlet class so-
javac –cp “D:\customtagjsp\servlet-api-2.5-6.1.5.jar” SampleHello.java
6-Now can see the .class file after compilation,Put it inside the classes folder of project structure and put projectfolder in side the webapps folder of Tomcat server of any version.
Here I am using apache-tomcat-7.0.47,
D:\apache-tomcat-7.0.47\webapps\projectfolder
7-Run the server and then run the project as below:-
http://localhost:8080/samplehello/
Now we can see the output as below:-
after submitting the form:-
0 comments:
Post a Comment