Professional Documents
Culture Documents
SERVLETS
SERVLETS
SERVLETS
Servlets are the Java programs that run on the Java-enabled web server or
application server. They are used to handle the request obtained from the
webserver, process the request, produce the response, then send a response back
to the webserver.
Properties of Servlets are as follows:
• Servlets work on the server-side.
• Servlets are capable of handling complex requests obtained from the
webserver.
ii) HEAD
This method is the same as the GET method. It is used to transfer the status line and
header section only.
HEAD /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.Medi-Caps.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
iii) POST
The POST request sends the data to the server. For example, file upload, customer
information, etc. using the HTML forms.
• send a form data to the server, which will be processed by a process.cgi and
finally a response will be returned:
POST /cgi-bin/process.cgi HTTP/1.1
iv) PUT
The PUT method is used to replace all the current representations of the target
resource with the uploaded content.
• requests the server to save the given entity-body in hello.htm at the root of
the server:
• PUT /hello.htm HTTP/1.1
v) DELETE
The DELETE method is used to remove all the current representations of the target
resource, which is given by URI.
• requests the server to delete the given file hello.htm at the root of the server:
DELETE /hello.htm HTTP/1.1
vi) CONNECT
The CONNECT method is used by the client to establish a network connection to a
web server over HTTP.
CONNECT www.Medi-Caps.com HTTP/1.1 User-Agent:Mozilla/4.0 (compatible;
MSIE5.01; Windows NT)
vii) OPTIONS
This method describes the options of communication for the target resource.
MVC
The Model-View-Controller (MVC) is a well-known design pattern in the web
development field. It is way to organize our code. It specifies that a program or
application shall consist of data model, presentation information and control
information.
In Java Programming, the Model contains the simple Java classes, the View used to
display the data and the Controller contains the servlets. Due to this separation the
user requests are processed as follows:
1. A client (browser) sends a request to the controller on the server side, for a page.
2. The controller then calls the model. It gathers the requested data.
3. Then the controller transfers the data retrieved to the view layer.
4. Now the result is sent back to the browser (client) by the view.
o MVC has the feature of scalability that in turn helps the growth of application.
o The components are easy to maintain because there is less dependency.
o A model can be reused by multiple views that provides reusability of code.
o The developers can work with the three layers (Model, View, and Controller) simultaneously.
o Using MVC, the application becomes more understandable.
o Using MVC, each layer is maintained separately therefore we do not require to deal with
massive code.
o The extending and testing of application is easier.
Init parameter
init parameters refers to the initialization parameters of a servlet or filter.
<init-param> attribute is used to define a init parameter. <init-param>
attribute has two main sub attributes <param-name> and <param-value>. The
<param-name> contains the name of the parameter and <param-value>
contains the value of the parameter. ServletConfig interface is used to access
the init parameters.
ServletContext Interface
An object of ServletContext is created by the web container at time of deploying the
project. This object can be used to get configuration information from web.xml file.
There is only one ServletContext object per web application.
If any information is shared to many servlet, it is better to provide it from the web.xml
file using the <context-param> element.
Advantage of ServletContext
Easy to maintain if any information is shared to all the servlet, it is better to make it
available for all the servlet. We provide this information from the web.xml file, so if
the information is changed, we don't need to modify the servlet. Thus it removes
maintenance problem.
1. The object of ServletContext provides an interface between the container and servlet.
2. The ServletContext object can be used to get configuration information from the web.xml
file.
3. The ServletContext object can be used to set, get or remove attribute from the web.xml file.
4. The ServletContext object can be used to provide inter-application communication.
Inter Servlet Communication
• A process where two or more servlets communicates with each other to
process the client request.
• A servlet can forward the request to another servlet to process the client
request.
• A servlet can include the output of another servlet to process the client
request.
Technique to do Inter-Servlet Communication
• Inter-servlet communication using Request Dispatcher
• A Request Dispatcher:
– Is an object of the javax.servlet.RequestDispatcher interface that allows
inter-servlet communication.
– Object is used to include the content of another servlet.
– Object is used to forward the request to another servlet.
• This interface is present in the javax.servlet package and contains only
following two methods :
– forward(ServletRequest request, ServletResponse response) Forwards
a request to another resource on the same server. That resource can be
a Servlet, JSP page or a simple HTML page.
– include(ServletRequest request, ServletResponse response) Works like
a server-side include ( SSI ) and includes the response from the given
resource ( Servlet, JSP page, HTML page ) within the caller response.
Servlet Listener
• A Servlet listener is a servlet method or function that awaits the occurrence
of an event.
• Servlet Listeners are classes that monitor a specific type of event and
activate functionality when that event happens.
• Servlet Listener works for listening to events in web containers like a
session is created, an attribute is inserted into a session, or a container is
activated.
• Servlet Listener is used for listening to events in web containers, such as
when you create a session, insert an attribute, passivate and activate in
another container. The servlet container generates events that trigger the
action of event listener classes.
There are total eight type of listeners available in servlet framework.
• ServletContextListener
• ServletContextAttributeListener
• HttpSessionListener
• HttpSessionAttributeListener
• ServletRequestListener
• ServletRequestAttributeListener
• HttpSessionActivationListener
• HttpSessionBindingListener
Servlet Filter
• A filter is an object that is invoked at the preprocessing and postprocessing
of a request on the server, i.e., before and after the execution of a servlet for
filtering the request. Filter API (or interface) includes some methods which
help us in filtering requests.
• To validate the data coming from the client because through the server the
data will be stored in the database. So, only valid data should enter the
database. Can we not do this validation (or add filters) on the client-side? We
can add filters on the client-side. But if javascript is disabled on the client-
side, then the request (or data) will not be checked. Someone might do this
intentionally. So to avoid this, we add Filters on the Server side.