You are on page 1of 24

The Servlet architecture

Web Technologies
Dynamic content generation
• Motivations
– Interactive applications require pages that contain
data that depends on the user’s need
• Data coming from databases
• Response to specific requests (e.g., queries)
– Client-side scripting does not achieve the required
results in dynamic data gathering
• Solution
– Server-side architectures that generate content
dynamically
HTTP basics
• HTTP is a stateless protocol:
– The client performs the request
– The web server responds and the transaction is done
• Each request is associated with a method, that
specifies the type of action the client wants
performed
• Available methods:
– GET
– POST
– Others: HEAD, PUT, DELETE, TRACE, OPTIONS
GET method
• The GET method is designed for getting
information (e.g., a document, a chart, the
result of a database query)
• The request contains some information that
describes what the user wants (e.g.,
coordinates x,y for a chart)
• This information is collected in the query
string, passed as a sequence of characters
appended to the request URLQuery string
http://my.server/page?par1=val1&par2=val2
POST method
• The POST method is designed for posting
information (a credit card number,
information that has to be stored in a
database)
• A POST request passes all its data, of
unlimited length, as part of its HTTP request
body
• The exchange is invisible to the client
– POST requests cannot be bookmarked or reloaded
POST method: form

Specify the method


<form method=POST action="my.page">
Tell me your name: This information is
sent to the server
<input type="text"
name="username"/>
<input type="submit"
value="Submit"/>
</form>
Other request methods
• HEAD: sent by a client when it wants to see only the
headers of the response
– Why? To determine document size, modification time,
general availability
• PUT: used to place documents directly on the server
• DELETE: used to remove documents from the server
• TRACE: returns to the client the exact content of its
request (used for debugging purposes)
• OPTIONS: used to ask the server which methods it
supports
Why do we use Java?
• Cross-platform: useful in case of a
heterogeneous collection of servers
(Unix/Windows operating systems)
• Object-oriented
• Support for networking and enterprise APIs
Servlets
• A servlet is a small, pluggable extension to a
server that enhances the server’s functionality
– Applications: web server, mail server, application
server…
• A servlet runs inside a JVM (Java Virtual Machine)
on the server
• Advantages
– Support for Java is required on servers (not in web
browsers)
– Servlets are portable (across operating systems and
web servers)
Servlet container

• A servlet container is a component of the


server that interacts with Java servlets
• It is responsible for:
– managing the lifecycle of the servlets
– mapping a URL to a particular servlet
JVM
HTTP request parameters
Servlet container

HTTP response response


Servlets
Persistence
• Servlets are all handled by separate threads
within the web server process
– A single object instance is stored in the server’s
memory
Request for Servlet1
Servlet container
thread
Request for Servlet2
thread Servlet1
Request for Servlet1
thread Servlet2

• Advantages of reusing processes:


– Servlets create stateful applications by storing
information about the user session
– Resources are shared, e.g., database connections
The Servlet API
• Servlets use classes and interfaces from two
packages
– javax.servlet: contains classes to support
generic servlets (protocol-independent)
– javax.servlet.http: adds HTTP-specific
functionality
• Every servlet implements the
javax.servlet.Servlet interface
– javax.servlet.GenericServlet is a
protocol-independent servlet
– javax.servlet.http.HttpServlet is an
HTTP servlet
GenericServlet

• This servlet overrides the service() method to


handle requests, taking as inputs:
– The request object
– The response object

Servlet1
request

service()
response
HttpServlet

• This servlet overrides the doGet() and doPost()


methods to handle GET and POST requests,
respectively
• The service() method handles the setup and
dispatching to all the doXXX() methods
– Do NOT override this method!
Servlet1
GET request
doGet()
GET response
POST request
service()
doPost()
POST response
Servlet life cycle
• Servlet’s initialization: when the server starts,
the servlet’s init() method is called
• Handle requests: when a request is captured by
the server, the servlet’s service(), doGet()
and doPost() methods are called according to
the request type
• Servlet’s destruction: when the server process is
stopped, the servlet’s destroy() method is
called and the garbage collection is performed
Hello World! servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World Servlet
</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
}
Hello World! servlet

import java.io.*;
import javax.servlet.*;
Packages HTTP servlet
import javax.servlet.http.*;
interface

public class HelloWorldServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World
Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet

import java.io.*; Request object


import javax.servlet.*; Response object
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World
Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse
response)
Set the standard MIME throws ServletException, IOException {
type for HTML pages response.setContentType("text/html");
PrintWriter out = response.getWriter();
A MIME type identifies the file
formats on the internet out.println("<HTML>");
A MIME type is used to out.println("<HEAD><TITLE>Hello World Servlet
understand how to interpret a </TITLE></HEAD>");
file/an attachment out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
Requires the writer on which the
response.setContentType("text/html");
output will be printed
PrintWriter out = response.getWriter();

out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World
Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

Print the web page HTML


out.println("<HTML>");
code
out.println("<HEAD><TITLE>Hello World
Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet results

Servlet’s path: project path + servlet’s name

Printed content
Configure a web application
• A web application is made of a set of servlets
that are stored in a project
• The description of the web application
content is contained in the web.xml file
• This file contains:
– The description of each servlet (name, class)
– The mapping of the servlet (used to reference the
servlet when accessing to the server)
Configuration file for HelloWorldServlet
Container for the servlets’ descriptions
<web-app>
<servlet>
<servlet-name>
HelloWorldServlet
</servlet-name>
<servlet-class>
it.polimi.tiw.examples.HelloWorldServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
HelloWorldServlet
</servlet-name>
Mapping to a specific
path on the server <url-pattern>
/HelloWorld
</url-pattern>
</servlet-mapping>
</web-app>

You might also like