Servlets - Introduction

 servelets 

                                                                                 An Introduction

What is servlet?

Servlets are java technology's answer to Common Gateway Interface(CGI)programming. They are programs that run on a web server, acting as a middle layer between a request  coming from a Web browser or other HTTP client and databases or applications on the HTTP server. Some of their jobs are as follows

  • Read any data sent by the user:

              we can read any data entered from a form or even from applet

  • Look up any other information about the request that is embedded in the HTTP request:

This information includes details of the browser capabilities, cookies, the host name of the requesting client etc.

  • Format the results inside a document.

The servlet also embedding the html tags in their responses.

  • Set the appropriate HTTP response parameters.

Informing the browser about the type of document is being returned (e.g.,HTML),setting and processing cookies and caching parameters etc.,

  • Send the document back to the client.

This document may be sent in text format (HTML), binary format (GIF images), or even in a compressed format that is layered on top of some other underlying format.


The Advantages of Servlets 


Java servlets are more efficient, easier to use, more   portable, safer, and cheaper than traditional CGI and manu alternative CGI-like technologies.

  • Efficient

  • Convenient

  • Powerful

  • Portable

  • Secure

  • Inexpensive


Architecture of the Servlet package

The javax.servlet package provides interface, either directly or, more commonly, by extending a class that implements it such as Http Servlet.

             

                                       




The Servlet interface declares, but does not implement, methods that manage the servlet and its communicatins with clients. Servlet writers provide some or all of these methods when developing a servlet.


Client Interaction


When a servlet accepts a call from a client, it receives two objects:

  • A servlet Request, which encapsulates the communication from the client to the server.

  • A Servlet Response, which encapsulates the communication from the servlet back to the client.


ServletRequest interface allows the servlet access to;


The Servlet Request Interface


The Servlet Request interface allows the servlet access to:


  • 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 input stream. Servlet InputStream, Servlets  use the inputg stream to get data from clients thatg use application protocols such as the HTTP POST and PUT methods.

Interfaces that extend Servlet Request interface allow the servlet to retrieve more protocol specific data. For example, the Http Servlet Request interface contains methods for accessing HTTP-specific header information.


The Servlet Response Interface 


 The Servlet Response 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.

  • Provides an output stream, Servlet OutputStream , and a Writer through which the servlet can send the reply data.

 

Interfaces that extend the ServletResponse interface give the servlet more protocol-specific capabilities. For example, the Http ServletResponse interface contains methods that allow the  servlet to manipulate HTTP - specific header information.


Additional Capabilities of HTTP Servlets


The classes and interfaces described above make up a basic Servlet. HTTP servlets have some additional objects that provide session-tracking capabilities. The servlet writer can use these APIS to maintain state between the servlet and the client that persists across multiple connections during some time period. HTTP servlets also have objects that provide cookies. The servlet writer uses the cookie API to save data with the client and to retrieve this data.


A Simple Servlet for a sample:


The following class completely defines servlet:


Example 1.1:

package testservletapplications;


import javax.servlet.*;


import javax.servlet. http. *;


import java.io.*;


public clas MyTestservlet extends HttpServlet

{


public void service(servletRequest req,ServletResponse res)  throws servletException ,IOException

{

         System.out.println("service method");

}


Public void doGet (HttpServletRequest request, HttpServletResponse response)

    throws servletException, IOException

{

printWriter out;

String title  = "simple Servlet Output";

// set content type 

response.setContent Type ("text/html" );

//then write the data of the response

out = response.getWriter ();

out.println ("<HTML><HEAD><TITLE>");


out.println (title);


out.println ("</TITLE></HEAD><BODY>");


out.println ("<H1>" + TITLE + "</H1>");


out.println ("<P>Hello World This is the simple Servlet");


out.println ("</Body></HTML>");


out. close ();

     }

}


No comments:

Post a Comment