Slot 3 - 4 - Filter and Annotation
Slot 3 - 4 - Filter and Annotation
API
Explain the steps to upload the file using HTML form elements
❖ Filters:
❑ Are Java classes.
❑ Used to encapsulate the preprocessing and post processing functionalities common to a
group of Servlets or JSP pages.
❑ Were introduced as a Web component in Java Servlet specification.
❑ Reside in the Web container along with the other Web components, such as Servlet or JSP
pages.
❖ Figure depicts filters in a Web application.
Compression of the content size sent from a Web server to a user over the
Internet.
Encryption of the request and response header for addressing security concerns.
❖ There can be more than one filter between the client and the Web resources,
thus forming a filter chain.
❖ Figure shows channing of the filters.
❑ A request or a response is passed through one filter to the next in the filter chain.
❑ Each request and response is serviced by filters configured in the chain, before the
Servlet is called by the Web container and after the Servlet responds.
The Web container instantiates the filters and loads them for preprocessing.
The filter intercepts the request from the user to the servlet.
Then, the filter sends the serviced request to the appropriate Servlet.
❖ When the response is returned to the client, the response passes through
same set of filters in reverse order.
❖ Figure depicts working of a typical filter.
Authentication
filters Allows the users to access any resource in secured Websites after
providing proper username and password.
Image conversion
filters
Scales the image size or change the image type as per requirements.
Data compression
filters Helps in compressing uploaded or downloaded file size, thereby
reducing the bandwidth requirement and time for downloading.
Encryption filters
Helps in encrypting the request and response header.
XML transformation Reads the XML data from the response and applies an XSLT
filters transformation before sending it to the client browser.
throws ServletException
❑ <description>
❑ <filter-name>
❑ <filter-class>
❑ <init-param>
• The element inside the deployment descriptor holds the name of the
<filter
filter to which a URL pattern or a Servlet is mapped.
-name> • It is a mandatory element for mapping a filter.
String getFilterName() Returns the name of the filter defined in the web.xml or
deployment descriptor file in the Web application.
❖ The code snippet shows the mapping for multiple patterns with the filter.
❖ The classes defined by Servlet API for wrapping request and response are as
follows:
❑ ServletRequestWrapper
ServletResponse interface.
Ease of Use
• Annotations are checked and compiled by the Java language compiler and
are simple to use.
Portability
• Annotations are portable.
Type Checking
• Annotations are instances of annotation types and are compiled in their own
class files.
Runtime Reflection
• Annotations are stored in the class files and accessed for runtime access.
The fields or methods of the main class that have been annotated for injection must
be static.
Servlet Filter
annotations annotations
Listener Security
annotations annotations
Attribute
Name Description
name Specifies the names of the initialization parameters.
value Specifies the value for the initialization parameters.
•javax.servlet.ServletContextAttributeList
Context Attribute Listener ener
•javax.servlet.http.HttpSessionAttributeLi
Http Session Attribute Listener stener
ServletSecurity
It is used on a Servlet class to specify security constraints to be
enforced by a servlet container on HTTP protocol messages.
HttpConstraint
It is used within the ServletSecurity annotation to represent
the security constraints to be applied to all HTTP protocol methods.
HttpMethodConstr
aint It is used within the ServletSecurity annotation to represent
security constraints on specific HTTP protocol messages.
• The value is provided when the form is using a file upload control.
multipart/form-data • It does not encode the characters before sending them in the
request.
text/plain • The value converts the spaces with the "+" symbols, however no
other special characters are encoded.
Filter and Annotation_Lương Hoàng Hướng 50
Client-side Uploading
❖ The code snippet shows the upload.jsp page containing the HTML form
element to upload the file on the server.
The maximum size of the data that can be uploaded on the server
The default location where the file will be uploaded on the server
maxFileSize • Specifies the maximum file limit allowed to upload the file
on the server.
maxRequestSize • Specifies the maximum file size in bytes to be accepted for
encrypted file data sent in the request.
• Specifies the default location for the file upload on the
location server.
Filter acts as an interface between client and servlet inside the server. It intercepts the request or
response to and from the server. More than one filter can also exist between a client and a servlet.
This is called a filter chain.
The Filter API is a part of the servlet package. It contain interfaces namely, Filter, FilterConfig,
and FilterChain.
All the information about the filter is described within the <filter> element inside the web.xml
file. The filter also needs to be mapped to the servlet for which it will function. This is done by
assigning URL name and servlet name inside the <filter-mapping> element.
The request and response to and from the servlet is manipulated by the filter. The wrapper object of
RequestWrapper or ResponseWrapper classes created by the filter intercepts the request or response
and sends it to the filter.
Annotation can be defined as metadata information that can be attached to an element within the code to
characterize it. Annotation can be added to program elements such as classes, methods, fields, parameters, local
variables, and packages.
The javax.servlet.annotation package contains a number of annotations Servlets, filters and listeners.
Apart from this, Servlet API also supports various dependency injection annotations.
File can be uploaded on the sever using the following two ways namely, Client-side file upload and