Unit Vi
Unit Vi
Unit Outcomes
Topics and Sub-topics
6.1 LifeCycle of Servlet
content to user
Example of Static Web Page:
(msbte server)
Web server maps this request to a specific file (index..html)
browser.
Header in the response indicates type of content(html/text).
information.
The data could be price, products for sale and it
must be updated.
User access this information via web page.
Gateway Interface(CGI).
C,C++ and Perl used to create CGI programs.
Introduction to Servlet
Problems with CGI:
connection
CGI programs are not platform-independent.
Introduction to Servlet
Problems with CGI:
connection
CGI programs are not platform-independent.
Introduction to Servlet
Advantages of Servlet:
Performance better
request
Platform independent as written in Java
Servlet
Communicate with Socket
Servlet
LIFE CYCLE OF SERVLET
Three methods are
servlet
Invoked at specific time.
LIFE CYCLE OF SERVLET
First Step
User enters URL in browser
Browser generates an HTTP Request to appropriate
Server
Second Step
The server received request and maps this request to a
specific Servlet.
The Servlet is dynamically retrieved and loaded into
server space.
Third Step
Server invoke init() method
init() method is invoked only when the servlet is first
loaded into memory(only once).
We can pass initialization parameters to the servlet for
their configuration
LIFE CYCLE OF SERVLET
Fouth Step
Server invoke service() method of Servlet
This method is invoked to process the HTTP request.
Servlet can read the data provided in HTTP request
Also we formulate HTTP Response for the client request
Servlet remains in address space of Server to process
any other HTTP Requests from other clients
The service() method is called for each HTTP Request.
Last Step
Server may decide to unload Servlet from its memory
At this time server calls destroy() method to relinquish
any resources such as file handles.
Requirement for Servlet
Programs
We need Servlet Container or Server
Popular Servers
https://tomcat.apache.org/)
Is an open source product
environment variable
WEB-INF
classes
o put your classfile here
web.xml (the important file where we write deployment
descriptor for Servlet)
Creating Simple Servlet
Web.xml
For a Java servlet to be accessible from a browser, you must tell the
servlet container what servlets to deploy, and what URL's to map the
servlets to.
This is done in the web.xml file of your Java web application.
First you configure the servlet. This is done using the <servlet>
element. Here you give the servlet a name, and writes the class name of
the servlet.
Second, you map the servlet to a URL or URL pattern. This is done in
Web.xm
l
First Example
import java.io.*;
import javax.servlet.*; / / Servlet API
public class First extends GenericServlet
{
public void service(ServletRequest req,ServletResponse
res) throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello generic servlet</b>");
out.print("</body></html>");
}
}
Example
javax.servlet
This package contains the classes and interfaces required to build servlets
First
Subclass of GenericServlet class which provides the functionality that simplifies the creation of
servlet
Don’t need to write init() and destroy() method of Servlet Lifecyle
Only service() method need to overwrite
throws IOException,ServletException
to build servlets.
1. javax.servlet
2. javax.servlet.http
javax.servlet
Package
Following table summarizes several key interfaces provided in
this package.
The most significant of these is Servlet interface
Servlet
Interface
All servlets must implements the Servlet interface.
It declares the init(), service() and destroy() methods that
developers.
However, the GET and POST requests are commonly used when
handling form input.
doGet() vs doPost()
Program
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyHttpServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
try{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<B>This is HTTP Servlet");
pw.close();
}catch(Exception e){}
}
}
Output