Program to demonstrate concept of servlet.

A servlet is a server-side program written in the Java programming language that interacts with clients and is usually tied to a HyperText Transfer Protocol (HTTP) server. One common use for a servlet is to extend a web server by providing dynamic web content.

Servlets are Java objects that implement the javax.servlet.Servlet interface. They are loaded by a servlet server such as Tomcat, Jetty, Resin, or BEA's WebLogic (to name but a few) which maps them to some location on the web server's web space. Any web access to that address is picked up by the servlet server and directed to the corresponding servlet, the servlet analyses the request and responds, usually with a html page containing the answer to the request, although more sophisticated response mechanisms are possible.

While most servlet servers can act as full http demons on their own, serving both servlets and standard web pages, they rarely have the range of web servicing facilities and tuned efficiency that a full web server like Apache, Netscape Server or Microsoft's Internet Information Server has. On the other hand, these web servers cannot provide the full servlet serving facilities that the servlet servers are dedicated to. Hence the usual configuration is to run a web server and a servlet server and configure the web server to forward servlet requests to the servlet server. The servlet server does not then act as a httpd demon.

Typically, rather than directly implement the javax.servlet.Servlet interface, servlets will extend javax.servlet.http.HttpServlet, which itself implements the Servlet interface. Much of the "boilerplate" work of extracting information from a request and putting together a response is already implemented for you in HTTPServlet. The web-based auction application uses a servlet to accept and process buyer and seller input through the browser and dynamically return auction item information to the browser. The AuctionServlet program is created by extending the HttpServlet class. The HttpServlet class provides a framework for handling HTTP requests and responses.

Servlets have an advantage over other technologies in that they are compiled, have threading capability built in, and provide a secure programming environment. Even web sites that previously did not provide servlet support, can do so now by using programs such as JRun or the Java module for the Apache web server.

Example:

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

public class Hello extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();

String name = req.getParameter("name");
out.println("");
Hello, " + name + " out.println("");//correct the spelling of title
out.println("");
out.println("Hello, " + name);
out.println("");
}

public String getServletInfo() {
return "A servlet that knows the name of the person to whom it's" +
"saying hello";
}
}

Methods of HttpServlet class
Let's examine the methods which this class provides one by one :
  • init()Called only once during the initialization of the Servlet.
  • destroy()Called only once when Servlet instance is about to be destroyed.
  • service()Do not override this method.
  • doGet(), doPost(), doPut(), doDelete(), doOptions, doTrace()These methods are called according to the type of HTTP request received. Override them to generate your own response.
  • log()Writes messages to the Servlet's log files.
  • getLastModified()Override this method to return your Servlet's last modified date.
  • getServletInfo()Override this method to provide a String of general info about your Servlet such author, version, copyright etc.
  • getServletName()Override this method to return name of the Servlet.
  • getInitParameter(), getInitParameterNames()First one returns value of given initialization parameter, second one returns an Enumeration object containing names of all initialization parameters provided
Also See life cycle of a servlet

No comments:

Google Search