Java Servlet
Java Servlet
Sabyasachi Moitra
[email protected]
What is Servlet?
O Servlets are small programs that execute on the server side
of a web application.
O It dynamically extends the functionality of a web server.
2
Servlet vs CGI
Servlet Common Gateway Interface
O Servlets run in one process, O CGI programs run as
i.e., for each request, a new separate processes on the
thread is created. computer, i.e., for each
O Servlets are platform request, a new process
independent. starts.
O More secured. O CGI programs are platform
dependent.
O Less secured.
3
Servlet API
javax.servlet javax.servlet.http
Package Package
Servlet
API
4
javax.servlet Package
Interface Class
Servlet GenericServlet
ServletConfig ServletInputStream
ServletContext ServletOutputStream
ServletRequest ServletException
ServletResponse UnavailableException
5
javax.servlet.http Package
Interface Class
HttpServletRequest Cookie
HttpServletResponse HttpServlet
HttpSession HttpSessionEvent
HttpSessionBindingListener HttpSessionBindingEvent
6
Servlet Interface
Method Description
public void init(ServletConfig config) • Initializes the servlet.
• It is the life cycle method of servlet.
• Invoked by the web container only
once.
public void service(ServletRequest • Provides response for the incoming
request,ServletResponse response) request.
• Invoked at each request by the web
container.
public void destroy() • Indicates that servlet is being
destroyed.
• Invoked only once.
7
Servlet Interface (contd…)
Method Description
public ServletConfig getServletConfig() Returns the object of ServletConfig.
public String getServletInfo() Returns information about servlet such
as writer, copyright, version etc.
8
GenericServlet Class
Method Description
public void init(ServletConfig config) Used to initialize the servlet.
public abstract void • Provides service for the incoming
service(ServletRequest request, request.
ServletResponse response) • Invoked at each time when user
requests for a servlet.
public void destroy() • Indicates that servlet is being
destroyed.
• Invoked only once throughout the life
cycle.
public ServletConfig getServletConfig() Returns the object of ServletConfig.
9
GenericServlet Class (contd…)
Method Description
public String getServletInfo() Returns information about servlet such
as writer, copyright, version etc.
public void init() • Convenient method for the servlet
programmers.
• No need to call super.init(config).
public ServletContext getServletContext() Returns the object of ServletContext.
public String getInitParameter(String Returns the parameter value for the
name) given parameter name.
public Enumeration Returns all the parameters defined in
getInitParameterNames() the web.xml file.
10
GenericServlet Class (contd…)
Method Description
public String getServletName() Returns the name of the servlet object.
public void log(String msg) Writes the given message in the servlet
log file.
public void log(String msg,Throwable t) Writes the explanatory message in the
servlet log file and a stack trace.
11
HttpServlet Class
Method Description
public void service(ServletRequest Dispatches the request to the protected
req,ServletResponse res) service method by converting the
request and response object into http
type.
protected void Receives the request from the service
service(HttpServletRequest req, method, and dispatches the request to
HttpServletResponse res) the doXXX() method depending on the
incoming http request type.
protected void doGet(HttpServletRequest • Handles the GET request.
req, HttpServletResponse res) • Invoked by the web container.
12
HttpServlet Class (contd…)
Method Description
protected void • Handles the POST request.
doPost(HttpServletRequest req, • Invoked by the web container.
HttpServletResponse res)
protected void • Handles the HEAD request.
doHead(HttpServletRequest req, • Invoked by the web container.
HttpServletResponse res)
protected void • Handles the OPTIONS request.
doOptions(HttpServletRequest req, • Invoked by the web container.
HttpServletResponse res)
protected void doPut(HttpServletRequest • Handles the PUT request.
req, HttpServletResponse res) • Invoked by the web container.
13
HttpServlet Class (contd…)
Method Description
protected void • Handles the TRACE request.
doTrace(HttpServletRequest req, • Invoked by the web container.
HttpServletResponse res)
protected void • Handles the DELETE request.
doDelete(HttpServletRequest req, • Invoked by the web container.
HttpServletResponse res)
protected long Returns the time when
getLastModified(HttpServletRequest req) HttpServletRequest was last modified
since midnight January 1, 1970 GMT.
14
Servlet Life Cycle
The servlet is initialized
by calling the init()
method
16
A Simple Servlet
(Using Notepad)
O Install Tomcat.
O Create a servlet source code (HelloServlet.java).
O Compile the servlet source code (javac HelloServlet.java -
classpath "C:\Program Files\Apache Software Foundation\
Tomcat 7.0\lib\servlet-api.jar")
O Copy the servlet’s class file (HelloServlet.class) to the proper
directory (C:\Program Files\Apache Software
Foundation\Tomcat 7.0\webapps\ServletPrograms\WEB-
INF\classes)
17
O Create web.xml file, add the servlet’s name & mappings to it,
& save it in the proper directory (C:\Program Files\Apache
Software Foundation\Tomcat
7.0\webapps\ServletPrograms\WEB-INF).
O Start the Tomcat.
O Open a web browser & request the servlet by entering the
URL shown below:
http://localhost:8080/ServletPrograms/HelloServlet
18
HelloServlet.java
19
web.xml
20
Output
21
Reading Servlet Parameters
(RSP.html)
22
PostParametersServlet.java
23
Output
24
POST vs GET
POST Method GET Method
Data is not displayed in the URL. Thus, Data is visible to everyone in the URL.
POST is a little safer than GET. Thus, less secure compared to POST.
http://localhost:8080/Servle http://localhost:8080/Servle
tsPrograms/ColorGetServlet tsPrograms/ColorGetServlet?c
olor=Blue
25
Handling HTTP POST Requests
(HHPR.html)
26
ColorPostServlet.java
27
Output
28
Handling HTTP GET Requests
(HHGR.html)
29
ColorGetServlet.java
30
Output
31
Cookie VS Session
Cookie Session
Stores user’s information to client’s side. Stores user’s information to server’s
side.
32
Using Cookies
(UC.html)
33
AddCookieServlet.java
34
Output
35
GetCookiesServlet.java
36
Output
37
Session Tracking
(DateServlet.java)
38
Output
39
References
O Courtesy of JavaTPoint – Servlet Tutorial. URL:
http://www.javatpoint.com/servlet-tutorial
O Courtesy of TutorialsPoint – Servlets Tutorial. URL:
http://www.tutorialspoint.com/java/servlets
O Herbert Schildt, Java: The Complete Reference, Seventh
Edition, TMGH, 2007
40