Servlet

indexadjustmentInternet and Web Development

Nov 13, 2013 (3 years and 5 months ago)

47 views

MCT620


Distributed Systems

Workshop 4


Lecture 3

Java Servlets

Component Platforms: JEE &.NET



Lecture 1


Container Component Platforms



Lecture 2


JSPs



Lecture 3


Servlets


In this Lecture ….


What are Servlets


Servlets : JSPs


All JSPs get converted to Servlets!


HTTP Protocol & Requests


Request / Response Interface


Interacting with Clients


Phases of a Servlet lifecycle


Servlets & Concurrency


How to create and use Sessions


Servlets & JEE

Servlets are also server based components
which can be accessed by a client.


Java code which writes output in HTML.


Accept requests from clients (usually web
browser).


Process these requests.


Return a Response to

the client.


What are Servlets ?

Servlets : JSPs


If JSPs are HTML pages interspersed with
Java code.


Servlets are java code which outputs HTML
tags and content.


Architecture of the Servlet

Package


The javax.servlet package

provides interfaces and

classes for writing Servlets.

All JSPs get converted to Servlets!

JSP

<html>

<head>

<title>Hello World
-

test the
Java EE SDK installation


</title>

</head>

<body>


<% for (int i = 1; i < 5; i++) {
%> <h<%=i%>>



Hello World

</h <%=i%>> <% } %>



</body> </html>

Servlet

out.write("<html>
\
n");
out.write("<head>
\
n");
out.write(" <title>Hello World
-

test the Java EE SDK
installation");


out.write(" </title>
\
n");
out.write("</head>
\
n");
out.write("<body>
\
n");


for (int i = 1; i < 5; i++) {
out.write("
\
n ");
out.write("<h");
out.write(String.valueOf(i));
out.write(">


Hello World"); out.write("</h");
out.write(String.valueOf(i));
out.write(">
\
n"); }


out.write("</body>
\
n");
out.write("</html>
\
n");

All JSPs get converted to Servlets!


That’s worth repeating:


Whenever JSPs are deployed and run they are compiled
into Servlets!


Don’t believe me?


Here’s the Proof:


Deploy and Run any JSP.


Then look in the ‘generated’ folder of your Glassfish
application server.


Find the folder for your app.


You should find some .java files correpsonding to your
JSP files.


Now, take a look at one


it should correspond the the
JSP.

HTTP Protocol & Requests


HTTP GET Method


HTTP POST Method


How do these relate to servlets?

The ServletRequest Interface


The ServletRequest interface allows the Servlet
access information such as the names of the
parameters passed in by the client, the protocol
(scheme) being used by the client, and the names
of the remote host that made the request and
the server that received it.


The ServletInputStream


Servlets use the input stream to get data from clients
that use application protocols such as the HTTP POST
and PUT methods.

Request / Response Interface


Interfaces that extend ServletRequest interface
allow the Servlet to retrieve more protocol
-
specific data.


For example, the HttpServletRequest interface
contains methods for accessing HTTP
-
specific
header information.


The ServletResponse Interface


The ServletResponse interface gives the Servlet
methods for replying to the client.


It allows the Servlet to set the content length
and MIME type of the reply.

Interacting with Clients


Interacting with Clients


An HTTP Servlet handles client requests through its
service method.


The service method supports standard HTTP client
requests by dispatching each request to a method
designed to handle that request.


The service method calls the doGet method in the simple
Servlet example shown earlier.


HttpServletRequest Objects


An HttpServletRequest object provides access to HTTP
header data.

HttpServletResponse Objects


HttpServletResponse Objects


An HttpServletResponse object provides two ways of
returning data to the user:


The getWriter method returns a Writer.


The getOutputStream method returns a ServletOutputStream.


Use the getWriter method to return text data to the
user and getOutputStream for binary data.


Closing the Writer or ServletOutputStream after you
send the response allows the server to know when
the response is complete.


Handling Get and Post Requests


The methods to which the service method
delegates HTTP requests include:


doGet, for handling GET, conditional GET, and HEAD
requests.


doPost, for handling POST requests.


doPut, for handling PUT requests.


doDelete, for handling DELETE requests.


Get and Post are the most common requests.


Your Servlet should override the method or methods
designed to handle the HTTP interactions that it
supports (otherwise an error is returned).

Servlets & Concurrency


A Servlet can handle multiple requests
concurrently, and can synchronise requests.


This allows Servlets to support systems such as
on
-
line conferencing.


Servlets can forward requests to other servers,
thus..


Servlets can be used to balance load among
several servers that mirror the same content, and
to partition a single logical service over several
servers.


Servlets


Threading Issues


HTTP Servlets are typically capable of serving
multiple clients concurrently.


If the methods in your Servlet that do work for clients
access a shared resource, then you can handle the
concurrency by creating a Servlet that handles only one
client request at a time.


You could alternatively synchronise access to the
resource, using synchronised methods etc.


To have your Servlet handle only one client at a
time, have your Servlet implement the
SingleThreadModelinterface.

Servlets


Threading Issues



public class ReceiptServlet
extends HttpServlet


implements SingleThreadModel

{



public void
doPost(HttpServletRequest request,


HttpServletResponse response)


throws
ServletException, IOException {


...


}


...


}

The Servlet Life Cycle


A server loads and initialises the Servlet.


The Servlet handles zero or more client
requests.


The server removes the Servlet (some servers
do this step only when they shut down).

How to create and use Sessions


HTTP is a stateless protocol


To deal with this Web apps use sessions.


A session is the entire interaction between a
client and server.


Two mechanisms:


Cookies


creates a file on client machine


URL writing


embeds session info in URL


HttpSession getSession() method

Servlets & JEE


Because Servlets run in a container, many
of the goals of Distributed Systems are
taken care of automatically:


Concurrency


Security


Scalability


Failure Recovery


Transaction Support

Summary


JSPs and Servlets provide a powerful
development and deployment platform for
web enabled applications.


But how do they manage to persist data as
is evident on any modern, enterprise
website?


Next week we find out all about databases
in distributed computing


in particular we
look at Java Database Connectivity or
JDBC.