Servlet
Servlet
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.
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
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
}
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.*;
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()
{
}
}