Web Technologies Lecture Notes Unit 5
Web Technologies Lecture Notes Unit 5
Web Technologies Lecture Notes Unit 5
Outline of Presentation
Tomcat Webserver
Introduction To Servlets
Life Cycle of Servlet
Servlet API
Reading Servlet Parameters
Steps to Run a Servlet
Example to handle Http Request and Response
Using Cookies and Session Tracking
Security Issues
Tomcat WebServer
A servlet container is like a mini server, but only
for serving html, jsp and servlets.
Many servlet containers could be used for this course.
Some may even be easier to configure than tomcat,
but tomcat provides an easy-to-use
development/deployment tool and also complies with
the servlet specification best of all containers.
Tomcat is from Apache and is open-source.
Tomcat can be used as a stand-alone servlet container.
You can install the Apache server with Tomcat, and
then proceed to configure each for their individual
purposes. (The server would relay servlet requests to
Tomcat.)
Introduction to Servlets
What can you build with Servlets?
Search Engines
E-Commerce Applications
Shopping Carts
Product Catalogs
Intranet Applications
Groupware Applications:
bulletin boards
file sharing
Perl 1
Browser 1
Browser 2
Web
Server
Perl 2
Browser N
Perl N
Browser 1
Browser 2
Browser N
Web
Server
Servlet
Portability
Like other Java technologies, servlet applications are portable.
Robustness
Servlets are managed by the Java Virtual Machine.
Don't need to worry about memory leak or garbage collection, which
helps you write robust applications.
Widespread acceptance
Java is a widely accepted technology.
Definitions
A servlet is a Java class that can be loaded
dynamically into and run by a special web server.
This servlet-aware web server, is known as servlet
container.
Servlets interact with clients via a request-response
model based on HTTP.
Therefore, a servlet container must support HTTP as
the protocol for client requests and server responses.
A servlet container also can support similar protocols
such as HTTPS (HTTP over SSL) for secure
transactions.
HTTP Request
Browser
HTTP Response
HTTP
Server
Servlet
Container
Static
Content
Servlet
Receive
Request
is servlet
loaded?
No
Yes
is servlet
current?
No
Load Servlet
Yes
Send
Response
Process Request
Initialization
init()
Service
service()
doGet()
doPost()
doDelete()
doHead()
doTrace()
doOptions()
Destruction
destroy()
Concurrent
Threads
of Execution
thread safe
Servlet APIs
Client
request
Server
service ( )
response
HTTPServlet
Browser
doGet( )
request
HTTP
Server
response
service ( )
doPost( )
Interface javax.servlet.Servlet
The Servlet interface defines methods
to initialize a servlet
Life
Cycle
to receive and respond to client requests
Methods
to destroy a servlet and its resources
to get any startup information
to return basic information about itself, such as its author,
version and copyright.
GenericServlet - Methods
void init(ServletConfig config)
Initializes the servlet.
void destroy()
Cleans up whatever resources are being held (e.g., memory, file
handles, threads) and makes sure that any persistent state is
synchronized with the servlet's current in-memory state.
ServletConfig getServletConfig()
Returns a servlet config object, which contains any initialization
parameters and startup configuration for this servlet.
String getServletInfo()
Returns a string containing information about the servlet, such as its
author, version, and copyright.
HttpServlet - Methods
void doGet (HttpServletRequest request,
HttpServletResponse response)
handles GET requests
void doPost (HttpServletRequest request,
HttpServletResponse response)
handles POST requests
void doPut (HttpServletRequest request,
HttpServletResponse response)
handles PUT requests
void doDelete (HttpServletRequest request,
HttpServletResponse response)
handles DELETE requests
HttpServletRequest - Methods
Enumeration getParameterNames()
an Enumeration of String objects, each String
containing the name of a request parameter; or
an empty Enumeration if the request has no
parameters
java.lang.String[] getParameterValues (java.lang.String name)
Returns an array of String objects containing
all of the values the given request parameter
has, or null if the parameter does not exist.
java.lang.String getParameter (java.lang.String name)
Returns the value of a request parameter as a
String, or null if the parameter does not exist.
HttpServletRequest - Methods
Cookie[] getCookies()
Returns an array containing all of the Cookie objects the
client sent with this request.
java.lang.String getMethod()
Returns the name of the HTTP method with which\thi
request was made, for example, GET, POST, or PUT.
java.lang.String getQueryString()
Returns the query string that is contained in the request
URL after the path.
HttpSession getSession()
Returns the current session associated with this request, or
if the request does not have a session, creates one.
HttpServletResponse - Methods
java.io.PrintWriter getWriter()
Returns a PrintWriter object that can send
character text to the client
void setContentType (java.lang.String type)
Sets the content type of the response being
sent to the client. The content type may
include the type of character encoding used,
for example, text/html; charset=ISO-8859-4
int getBufferSize()
Returns the actual buffer size used for the
response
Servlet Example
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
14:
16:
17:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req,
HttpServletResponse res)
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println( "<HTML><HEAD><TITLE> Hello You! +
</Title></HEAD> +
<Body> HelloYou!!!</BODY></HTML> );
out.close();
}
}
Deployment Descriptor
The deployment descriptor is a XML file called web.xml that resides in the WEBINF directory whitin an application.
<web-app xmlns=http://java.sun.com/xml/ns/j2ee>
<display-name>test</display-name>
<description>test example</description>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>/servlet/TestingServlet</url-pattern>
</servlet-mapping>
</web-app>
Servlet Cookies
Cookies are text files that store sets of param/value
pairs. The Servlet at the server side generates the
cookie based on the clients HTTP request.
The cookie is created on the server side and sent back
to the client along with the HttpServletResponse
object.
The cookie is stored in the clients browser.
URL Rewriting
Security Issues
Server-side Security Issues
User Authentication
Logging of Sensitive Information
Importanat questions
1. Briefly explain about Tomcat web server.
2 a) What are the limitations of Servlets?
b) Explain Servlet Vs CGI
3. Explain the life cycle of a servlet.
4. Write a session tracker that tracks the number of accesses and last access
data of a particular web page.
5.a) Discuss about javax.servelet package.
b) What are the security issues related to servlets.