Dynamic Web Paging Using: Java Servlet
Dynamic Web Paging Using: Java Servlet
Java Servlet
Request
Web Web
Browser Server Servlet
Response
Why Servlet?
• Before Servlet there was a CGI (common gateway interface)
to create a
dynamic webpages with all languages like c++, java etc.
• Working Method of CGI:
– When Client makes a URL Request for dynamic web
page to CGI, CGI will create a individual Process for
each and every request which is not implemented on
Server.
• Drawbacks of CGI:
– Low Performance
– Server is not utilized
– Native Interface: so, Not Portable
• So, Java Servlet is released by Sun Microsystems.
Advantages of Servlet
• Efficiency
o More efficient – uses lightweight java threads as
opposed to individual processes
• Persistency
o Servlets remain in memory
o Servlets can maintain state between requests
• Portability
o Since servlets are written in Java, they are platform
independent
• Robustness
o Error handling, Garbage collector to prevent problems
with memory leaks
o Large class library – network, file, database, distributed
object components, security, etc.
Advantages….
• Extensibility
– Creating new subclasses that suite your needs
• Inheritance, polymorphism, etc.
• Security
– Security provided by the server as well as the Java Security
Manager
• Powerful
– Servlets can directly talk to web server
– Facilitates database connection pooling, session tracking
etc.
• Convenient
– Parsing and decoding HTML form data, reading and setting
HTTP headers, handling cookies, etc.
Java Servlet Architecture
• Java Servlet Architecture provided by Servlet
API.
• Following are two packages provides
Architecture to the Servlet.
– javax.servlet
• Contains generic interfaces and classes that are
implemented and extended by all servlets
– javax.servlet.http
• Contains classes that are extended when creating HTTP-
specific servlets
Servlet Implementation
• Implementation of servlet can be done using
following 4 ways:
– Servlet Interface
– GenericServlet Class
– HttpServlet Class
– Single Thread Model
Servlet Interface
• Servlet Interface provides life cycle to the servlet.
• So we have to compulsory implement this interface
into the our servlet class.
• Servlet interface has following methods:
1. Public void init (ServletConfig config) throws
ServletException
2. Public void service(ServletRequest req, ServletResponse
res) throws ServletException, IOException
3. Public void destroy()
4. Public ServletConfig getServletConfig()
5. Public String getServletInfo()
GenericServlet Class
• When we implemented servlet interface we
must to override all methods of that interface
into our servlet class
• So for that an Abstract GenericServlet class is
created.
• In that three interfaces are implemented:
– Servlet Interface javax.servlet
– ServletConfig Interface javax.servlet
– Serializable Interface java.io
GenericServlet…
<<Interface>>
<<Interface>> javax.servlet.ServletConfig
javax.servlet.Servlet <<Interface>>
javax.io.Serializable getInitParameter( )
init( ) getServletContext( )
getServletConfig( ) getInitParameterNames( )
service( ) getServletName( )
getServletInfo( )
destroy( )
Implemented
javax.servlet.GenericServlet
init( )
getServletConfig( )
service( )
getServletInfo( )
destroy( )
getInitParameter( )
getServletContext( )
getInitParameterNames( )
getServletName( )
log( )
Extends
New Servlet
Simple Servlet Program
import java.io.*;
import javax.servlet.*;
public class SimpleServlet extends GenericServlet
{
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<B><I> Example of Generic Servlet Class");
out.close();
}
}
Servlet Life Cycle
If servlet is already loaded
Servicing requests by
calling the
service method
Calling the
init method
Destroying the servlet
ServletConfig by calling the
Initialization destroy method
and Loading
Garbage
Servlet Class Collection
Servlet Life Cycle….
Life Cycle of Servlet describes followings three methods
init()
public void init(ServletConfig config) throws
ServletException
service()
public void service(ServletRequest req, ServletResponse
res) throws ServletException, IOException
destroy()
public void destroy()
These methods are provided by Servlet Interface of javax.servlet
package.
• The init() method is called only once by the servlet
container throughout the life of a servlet.
• Parameters - The init() method takes a
ServletConfig object that contains the initialization
parameters and servlet's configuration and throws
a ServletException if an exception has occurred.
• Once the servlet starts getting the requests, the
service() method is called by the servlet container
to respond.
• Parameters - The service() method takes the
ServletRequest object that contains the client's
request and the object ServletResponse contains
the servlet's response.
• The service() method throws ServletException and
IOExceptions exception.
• destroy() method is called when we need to close
the servlet.
• That is before removing a servlet instance from
service, the servlet container calls the destroy()
method.
• Once the servlet container calls the destroy()
method, no service methods will be then called .
• That is after the exit of all the threads running in
the servlet, the destroy() method is called.
• Hence, the servlet gets a chance to clean up all the
resources like memory, threads etc which are
being held.
Life cycle of servlet
import java.io.*;
import javax.servlet.*;
public class ServletLifeCycle extends GenericServlet
{
int count;
public void init(ServletConfig config) throws ServletException
{
count =1;
}
public void service(ServletRequest req,ServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/plain");
PrintWriter out=res.getWriter();
out.println("Value of count is :"+count++);
}
public void destroy()
{
System.out.println("Destroyed");
}
}
HttpServlet Class
• HttpServlet class is a member of
javax.servlet.http package
• Basic differences between GenericServlet class
and HttpServlet Class are:
– GeneriServlet class provides protocol independent
Servlet transaction. Whereas HttpServlet class
provides protocol depended (HTTP) servlet
transcation.
– GenericServlet can not handle http request and
response
– So, GenericServlet can not provide handle session
tracking approaches like cookie and session.
HttpServlet… <<Interface>>
<<Interface>> javax.servlet.ServletConfig
javax.servlet.Servlet <<Interface>>
javax.io.Serializable getInitParameter( )
init( ) getServletContext( )
getServletConfig( ) getInitParameterNames( )
service( ) getServletName( )
getServletInfo( ) Implements
destroy( )
javax.servlet.GenericServlet
init( )
getServletConfig( )
service( )
getServletInfo( )
destroy( )
getInitParameter( )
getServletContext( )
getInitParameterNames( )
getServletName( )
log( )
Extends
javax.servlet.http.HttpServlet
doDelete( )
doGet( )
doOptions( )
doPost( )
doPut( )
doTrace( )
getLastModified( )
service( )
Extends
New Servlet
HttpServlet Class
• Http request can be done using two ways:
– Get Method
– Post Method
Stub:-
The stub is an object, acts as a gateway for the client side. All the outgoing requests are
routed through it. It resides at the client side and represents the remote object. When the
caller invokes method on the stub object, it does the following tasks:
1. It initiates a connection with remote Virtual Machine (JVM),
2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception,
5. It finally, returns the value to the caller
Skeleton:-
The skeleton is an object, acts as a gateway for the server side object. All the
incoming requests are routed through it. When the skeleton receives the incoming
request, it does the following tasks:
Stub − A stub is a representation (proxy) of the remote object at client. It resides in the client
system; it acts as a gateway for the client program.
Skeleton − This is the object which resides on the server side. stub communicates with this
skeleton to pass request to the remote object.
RRL(Remote Reference Layer) − It is the layer which manages the references made by the
client to the remote object.