Servlet technology is used to create web application (resides at server side and
generates dynamic web page).
What is servlet ?
1. Servlet is an API that provides many interfaces and classes including
documentations.
2. Servlet is an interface that must be implemented for creating any servlet.
3. Servlet is a class that extend the capabilities of the servers and respond to the
incoming request. It can respond to any type of requests.
4. Servlet is a web component that is deployed on the server to create dynamic
web page.
Advantage of Servlet
1.Better performance: because it
creates a thread for each request not
process.
2.Portability: because it uses java
language.
3.Robust: Servlets are managed by
JVM so we don't need to worry
about memory leak, garbage
collection etc.
4. Secure: because it uses java
language..
Servlet Terminology
1. Website
2. HTTP
HTTP is media independent
HTTP is connectionless
HTTP is stateless
3. HTTP Requests
What kind of request ?
The Request-line
The analysis of source IP address, proxy and port
The analysis of destination IP address, protocol, port
and host
The Requested URI (Uniform Resource Identifier)
The Request method and Content
The User-Agent header
The Connection control header
The Cache control header
HTTP Request Description
GET Asks to get the resource at the requested URL.
POST Asks the server to accept the body info attached. It is like GET request with extra
info sent with the request.
HEAD Asks for only the header part of whatever a GET would return. Just like GET but
with no body.
TRACE Asks for the loopback of the request message, for testing or troubleshooting.
PUT Says to put the enclosed info (the body) at the requested URL.
DELETE Says to delete the resource at the requested URL.
OPTIONS Asks for a list of the HTTP methods to which the thing at the request URL can
respond
It remains in the browser
It remains in the browser history
history
It can be bookmarked
It can be bookmarked
It can be cached
It can be cached
It have length restrictions
It have length restrictions
It should never be used when dealing
It should never be used when
with sensitive data
dealing with sensitive data
It should only be used for retrieving
It should only be used for
the data
retrieving the data
4. Servlet
Container
1 The servlet container is used in java for dynamically generate the web pages on the
server side
The Servlet Container performs many
operations
1. Life Cycle Management
2. Multithreaded support
3. Object Pooling
4. Security etc.
5. Content Type
text/html ,text/plain ,application/msword ,application/vnd.ms-excel
,application/jar ,
application/pdf , application/octet-stream ,application/x-zip, images/jpeg ,
images/png
images/gif ,audio/mp3 , video/mp4 , video/quicktime etc.
6. Servlet API
1. javax.servlet
2.
javax.servlet.http
Classes
Interfaces in javax.servlet
GenericServlet
Servlet
ServletInputStream
ServletRequest
ServletResponse
ServletOutputStream
RequestDispatcher ServletRequestWrapper
ServletConfig ServletResponseWrapper
ServletContext ServletRequestEvent
SingleThreadModel ServletContextEvent
Filter ServletRequestAttributeEv
FilterConfig ent
FilterChain
ServletContextAttributeEv
ServletRequestListener
ServletRequestAttributeListe
ent
ner ServletException
ServletContextListener UnavailableException
ServletContextAttributeListe
ner
Interfaces in javax.servlet.http Classes in
HttpServletRequest javax.servlet.http
HttpServletResponse HttpServlet
HttpSession Cookie
HttpSessionListener HttpServletRequestWrapper
HttpSessionAttributeListener HttpServletResponseWrapper
HttpSessionBindingListener HttpSessionEvent
HttpSessionActivationListener HttpSessionBindingEvent
HttpSessionContext (deprecated
now) HttpUtils (deprecated now)
Life Cycle of a Servlet
1. Servlet class is loaded -- first request
2. Servlet instance is created – only once
3. init method is invoked - - only once
4. service method is invoked --each time
servlet
request
5. destroy method is invoked – calls before
removing the servlet instance from the
service
How servlet works
Servlet Interface
Method Description
public void initializes the servlet. It is the
init(ServletConfig config) life cycle method of servlet and
invoked by the web container
only once.
public void provides response for the
service(ServletRequest incoming request. It is invoked
request,ServletResponse at each request by the web
response) container.
public void destroy() is invoked only once and
indicates that servlet is being
destroyed.
public ServletConfig returns the object of
getServletConfig() ServletConfig.
public String returns information about
getServletInfo() servlet such as writer,
copyright, version etc.
import java.io.*;
import javax.servlet.*;
public class First implements Servlet{
ServletConfig config=null;
public void init(ServletConfig config){
this.config=config;
System.out.println("servlet is initialized");
}
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 simple servlet</b>");
out.print("</body></html>");
}
public void destroy(){System.out.println("servlet is destroyed");}
public ServletConfig getServletConfig(){return config;}
public String getServletInfo(){return "copyright 2007-1010";}
}
GenericServlet class
1. GenericServlet class implements Servlet, ServletConfig and Serializable interfa
2. GenericServlet class can handle any type of request so it is protocol-independ
METHODS :
1. public void init(ServletConfig config)
2. public abstract void service(ServletRequest request, ServletResponse response
3. public void destroy()
4. public ServletConfig getServletConfig()
5. public String getServletInfo()
6. public void init()
import java.io.*;
import javax.servlet.*;
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>");
}
}
HttpServlet class
1. It provides http specific methods such as doGet, doPost, doHead, doTrace etc
MATHODS :
1. public void service(ServletRequest req,ServletResponse res)
2. protected void service(HttpServletRequest req, HttpServletResponse res)
3. protected void doGet(HttpServletRequest req, HttpServletResponse res)
4. protected void doPost(HttpServletRequest req, HttpServletResponse res)
5. protected void doHead(HttpServletRequest req, HttpServletResponse res)
6. protected void doOptions(HttpServletRequest req, HttpServletResponse res)
7. protected void doPut(HttpServletRequest req, HttpServletResponse res)
8. protected void doTrace(HttpServletRequest req, HttpServletResponse res)
9. protected void doDelete(HttpServletRequest req, HttpServletResponse res)
10.protected long getLastModified(HttpServletRequest req)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class abc extends HttpServlet {
private String message;
public void init() throws ServletException
{
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
}
}