Reading:  

Introduction to Servlet technology


Servlet Filters and exception handling

Servlet Filters in Java classes are used for following purposes:

  • To check requests from client before they access resource at back end.
  • To check responses from server before sent back to client.

The various types of filters are:

  • Image Conversion Filters
    Logging and Auditing Filters
    MIME-TYPE Chain Filters
    Tokenizing Filters
    Authentication Filters
    Data compression Filters
    Encryption Filters

Filters are deployed in deployment descriptor file web.xml and then map to URL patterns or servlet names in application's deployment descriptor.

Servlet Filter Methods

The javax.servlet.Filter interface implements filter methods:

Method & Description

public void doFilter (ServletRequest, ServletResponse, FilterChain)
The method is called by container each time a response/ request pair is passed through chain .

public void init(FilterConfig filterConfig)
The web container call method to indicate a filter is being placed into service.

public void destroy()
The web container call method to indicate a filter is being taken out of service.

 

Exception handling 

When servlet throws an exception, web container searches configurations in web.xml that use exception-type element for a match with thrown exception type. 

web.xml Configuration

The ErrorHandler servlet will be called whenever there is any defined exception or error. Below is the entry created in web.xml.

<!-- definition servlet -->
<servlet>
        <servlet-name>ErrorHandler</servlet-name>
        <servlet-class>ErrorHandler</servlet-class>
</servlet>
<!—mappings servlet -->
<servlet-mapping>
        <servlet-name>ErrorHandler</servlet-name>
        <url-pattern>/ErrorHandler</url-pattern>
</servlet-mapping>
<error-page>
    <error-code>404</error-code>
    <location>/ErrorHandler</location>
</error-page>
<error-page>
    <error-code>403</error-code>
    <location>/ErrorHandler</location>
</error-page>
<error-page>
    <exception-type>
          javax.servlet.ServletException
    </exception-type >
    <location>/ErrorHandler</location>
</error-page>

<error-page>
    <exception-type>java.io.IOException</exception-type >
    <location>/ErrorHandler</location>
</error-page>

There can be generic Error Handler for all exceptions rather than having separate error-page elements for every exception:

<error-page>
    <exception-type>java.lang.Throwable</exception-type > 
   <location>/ErrorHandler</location>
</error-page>
 

Following are the points to be noted about above web.xml for Exception Handling:

  • The servlet ErrorHandler is declared in same way as like other servlet and configured in web.xml.
  • For the HTTP status code 404 (Not Found) or 403 (Forbidden) also ErrorHandler servlet is called.
  • If web application throws either IOException or ServletException then web container invokes /ErrorHandler servlet.

Request Attributes - Errors/Exceptions

Below is list of request attributes for an error-handling servlet:

Method & Description

javax.servlet.error.status_code

It gives attribute status code in a java.lang.Integer data type.

javax.servlet.error.exception_type

It gives attribute information about exception type in a java.lang.Class data type.

javax.servlet.error.message

It gives attribute information about error message in a java.lang.String data type.

javax.servlet.error.request_uri

It gives attribute information about URL in a java.lang.String data type.

javax.servlet.error.exception

It gives attribute information about exception raised in a java.lang.Throwable data type.

javax.servlet.error.servlet_name

It gives attribute information about servlet name in a java.lang.String data type.

 

Description

This guide introduces Servlet technology and we will cover below topics

  1. What is a Servlet?
  2. Initial Setup
  3. Life Cycle
  4. Various Examples
  5. Client Request
  6. Server Response
  7. Http Status Codes
  8. Writing Filters
  9. Exception Handling
  10. Cookies Handling
  11. Session Tracking
  12. Database Access
  13. Packaging
  14. Internationalization

This is to the point introduction to the topic to get you started

 



Prerequisites

Working knowledge of Core Java is essential

Audience

Beginner to Java Servlet technology or students looking to brush up their skills quickly

Learning Objectives

Learn Java Servlets

Author: Subject Coach
Added on: 2nd May 2015

You must be logged in as Student to ask a Question.

None just yet!