Java
Servlets are server side components that provide a powerful mechanism for
developing server side of web application by means of a request- response
programming model.
It
has two types:
- GenericServlet: Defines a generic or protocol independent servlet
- HttpServlet: HttpServlet is subclass of GenericServlet and provides some http specific functionality like doGet and doPost methods.
Servlets Architecture
- Read the explicit data sent by the clients (browsers)
- Read the implicit HTTP request data sent by the clients (browsers)
- Process the data and generate the results.
- Send the explicit data (i.e., the document) to the clients (browsers)
- Send the implicit HTTP response to the clients (browsers)
Servlets Packages
- Servlets can be created using any of the two packages: which are a standard part of the Java's enterprise edition
o
javax.servlet
o
javax.servlet.http
Servlets Life Cycle:
Servlet life cycle can be defined as the entire process from its creation till the destruction:- The servlet is initialized by calling the init() method.
- The init method is designed to be called only once. It is called when the Servlet is first created, and not called again for each user request.
- The Servlet calls service() method to process a client's request.
- The service() method is the main method to perform the actual task. The Servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.
- The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
- So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.
- The Servlet is terminated (garbage collected) by calling the destroy() method.
- The destroy() method is called only once at the end of the life cycle of a Servlet. This method gives your Servlet a chance to close database connections, halt background threads, write cookie lists, and perform other such cleanup activities.
- After the destroy() method is called, the Servlet object is marked as garbage collection
- The destroy () method is only called when the container is shut down, not after processing each request.
- When you close your browser window the destroy () method doesn’t get called. You just close your session with the server.
Servlet Filters
- A filter is simply a Java class that implements the javax.servlet.Filter interface, used for following purposes:
- To intercept requests from a client before they access a resource at back end.
- To manipulate responses from server before they are sent back to the client.
- There are various types of filters:
- Authentication Filters.
- Data compression Filters
- Encryption Filters.
- Filters that trigger resource access events.
- Image Conversion Filters.
- Logging and Auditing Filters.
- MIME-TYPE Chain Filters.
- Tokenizing Filters.
- XSL/T Filters That Transform XML Content.
- Filters are deployed in the deployment descriptor file web.xml and then map to either Servlet or JSP names or URL patterns in your application's deployment descriptor.
- When the JSP container starts up your web application, it creates an instance of each filter that you have declared in the deployment descriptor. The filters execute in the order that they are declared in the deployment descriptor.
- Example: Filter Mapping in Web.xml
- Filters are defined and then mapped to a URL or JSP file name, in much the same way as Servlet is defined and then mapped to a URL pattern in web.xml file.
<filter>
<filter-name>LogFilter</filter-name>
<filter-class>LogFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization
Paramter</param-value>
</init-param>
</filter>
<filter>
<filter-name>AuthenFilter</filter-name>
<filter-class>AuthenFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization
Paramter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AuthenFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- To reverse the order of the filter, you just need to reverse the filter-mapping elements in the web.xml file above.
- Note: Servlets and JSP filters are same thing
Servlet
Class Hierarchies:
Servlet Class Hierarchy |
Note: Tomcat is an example of a
Container
Your Java blog has explained the concepts clearly with suitable examples. Many programming languages have evolved but Java is still in NO: 1 position due to its great features.
ReplyDeleteRegards:
java training in velachery
java training institute in chennai