|
| 1 | +## The servlet can be created by three ways: |
| 2 | +* By implementing Servlet interface, |
| 3 | +* By inheriting GenericServlet class, (or) |
| 4 | +* By inheriting HttpServlet class |
1 | 5 |
|
2 |
| -# Servlet Life Cycle |
| 6 | +### GenericServlet |
| 7 | +* Implements Servlet, ServletConfig andSerializable interface. |
| 8 | +* It provides the implementaion of all the methods of these interfaces except the service method. |
| 9 | +* GenericServlet class can handle any type of request so it is protocol-independent. |
3 | 10 |
|
4 |
| - |
| 11 | +### HttpServlet |
| 12 | +* class extends the GenericServlet class and implements Serializable interface. |
| 13 | +* It provides http specific methods such as doGet, doPost, doHead, doTrace etc |
5 | 14 |
|
6 |
| -* If an instance of the servlet does not exist, the Web container loads the class. |
7 |
| -* Creates an instance of the servlet class. |
8 |
| -* Initializes the servlet instance by calling the init method. |
9 |
| -* Invokes the service method, passing a request and response object, For Http Servlet, Service method invokes doGet, doPost, etc. methods as appropriate, Override only doGet or doPost etc. method as needed. |
10 |
| -* Finalize the request by calling Destroy method. |
| 15 | +## Package and Deployment |
| 16 | + <p align="center"> |
| 17 | + <img src="https://github.com/SuneetPatil/Java-Servlets/blob/master/Images/PackageandDeployment.png" alt="Package and Deployment"> |
| 18 | + </p> |
| 19 | + |
| 20 | +### Deployment Descriptor : |
| 21 | +* web.xml is the deployment descriptor for web applications. |
| 22 | +* Every web application module must have a single web.xml file in the folder root/WEB-INF |
| 23 | + |
| 24 | +``` |
| 25 | +<web-app> |
| 26 | + <servlet> |
| 27 | + <servlet-name>GenericServlet</servlet-name> |
| 28 | + <servlet-class>com.suneet.GenericServletExample</servlet-class> |
| 29 | + </servlet> |
| 30 | + <servlet-mapping> |
| 31 | + <servlet-name>GenericServlet</servlet-name> |
| 32 | + <url-pattern>/exampleservlet</url-pattern> |
| 33 | + </servlet-mapping> |
| 34 | +</web-app> |
| 35 | +
|
| 36 | +``` |
0 commit comments