JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content...

Post on 18-Jan-2016

215 views 1 download

Transcript of JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content...

JS (Java Servlets)

Internet evolution [1]

The internet Internet started of as a static

content dispersal and delivery mechanism, where files residing on servers anywhere in the world can be served to a browser connected to the internet using the HTTP protocol

The need for sharing dynamic content (time variant, person variant etc.,) led to the evolution of the CGI (Common Gateway Interface) mechanism

CGI programs were originally written on C and generate HTML dynamically based on the request passed to it from the server.

– Problems with CGI were• CGI model was very slow, as it was

based on starting new processes for each client request. CPU speed and memory capacity became bottlenecks

• Tasks performed by the programs such as opening database connections had to be repeated every time

• Merging of static presentation and dynamic business logic affected application development turnaround times

Internet evolution [2]

Sun’s response Sun’s response to this

technological problem was the servlet technology

Servlet architecture eliminates the need for reloading a servlet for every browser request. The JVM stays running and handles each servlet request using a lightweight Java thread, and not a heavyweight operating system process as is the case with CGI programs

If request is made for a servlet N times, there will be N servlet threads but only a single copy of the servlet class.

– The servlet technology revolutionized web application development using Java technologies

Introduction to Java servlets

Servlets introduction Servlets are the Java counter

part to non-Java dynamic web content technologies such as CGI and ASP.NET.

Java EE provides the Java Servlet API, which is a protocol by which a Java class may respond to HTTP requests

A Java servlet is a Java class that conforms to the Java Servlet API

Servlets are generally not tied to any specific client-server protocol, but are most often used with HTTP protocol

The word “Servlet” is often used in the meaning of HTTP servlet

A software developer may use a Servlet to add dynamic content to a Web server (java enabled) using the Java platform.

• The generated content is commonly HTML, but may be XML as well

The servlet environment

Servlets environment Servlets are Java classes

that can be loaded dynamically and run by a Java enabled Web server.

The Web server provides support for servlets with extensions called servlet engines.

Browsers interact with the servlet using the HTTP request / response protocol

Servlet container provides the following services and functionality

• The network services over which the requests and responses are sent

• Registers the servlet against one or more URLs

• Manages the servlet lifecycle• Decodes MIME based requests• Constructs MIME based responses• Supports the HTTP protocol• A Servlet container can also enforce

security restrictions on the environment, such as requesting to login to access specific web pages

Servlet lifecycle diagram

InitDemo.java code demonstrates that the init() method is called only once and the doGet() method is called with every invocation of the servlet.

The servlet execution environment (server)

Execution environment The servers use

ServletConfig interface to pass initialization and context information to servlets, which consists of

Initialization information consists of a series of initialization parameters (init parameters) and

a ServletContext object, that provides information about the server / execution environment

• The ServletContext interface defines a series of methods that can be used to communicate with the server in non-request specific manner.

• This includes the following• Finding path information• Accessing other servlets

running on the server• Writing to the server log file.

• ServerInfo.java code

The servlet API

The servlet API The central abstraction in

the servlet API is the Servlet interface. All servlets implement this interface directly or more commonly by extending a class that implements the same

Most servlets extend one of the standard implementations of that interface viz., javax.servlet.GenericServlet or javax.servlet.HttpServlet

• Unlike a regular Java program, a servlet does not have a main() method.

• Instead, certain methods of a servlet are invoked by the server in the process of handling requests.

• Each time the server dispatches a request to a servlet, it invokes the servlets service() method

Servlet API diagram

A generic servlet should override its service() method to handle requests as appropriate for the servlet.

The service() method accepts two parameters, request object and a response object. The request object tells the servlet about the request, while the response object is used to return a response.

A HTTP servlet instance usually does not override the service() method.

Instead, it overrides doGet() to handle GET requests and doPost() to handle POST requests.

A HTTP servlet can override either or both of these methods depending on the type of requests it needs to handle.

The service() method of HttpServlet handles the setup and dispatching to all the doXXX() methods, which is why it should not be overwritten.

The HTTP Request (client information)

Client information For each HTTP request, a

servlet has the ability to find out about the client machine making the request from the HttpServletRequest object

This information can be used to used for logging access data or restricting access to certain clients

A Servlet can use getRemoteAddr() and getRemoteHost() on the request object to retrieve the IP address and the host name of the client machines

• It is possible to retrieve all information related to the request, such as

• The request method by using getMethod()

• The URL as sent by the client by using getRequestURL()

• The query string (if it exists) by using getQueryString() method

• Any HTTP header details by using the getHeader() method (e.g. request.getHeader(“User-Agent”))

• ClientInfo.java code

The HTTP Response

HTTP response A response from a web

server to a HTTP request contains the following

Status line Some response headers A blank line followed by The body of the response

As seen from the previous two examples, the HTTP response has to be built by the servlet

First step should be to set the content type of the response by using the setContentType() method

• The servlet should get a PrintWriter object using the getWriter() method. The PrintWriter object will enable sending of character text to the client

• The body of the response, viz., the HTML text is created by a sequence of out.println statements with the appropriate text as required

• This is the simple form of generating a HTTP response

• There are other things possible in the response – such as indicating an error condition, setting various HTTP headers as part of the HTTP response to control application behaviour on the client side.