Wednesday 8 August 2012

Java Servlets - Basics

Servlets are java technology’s answer to CGI programming. CGI was widely used for generating dynamic content before Servlets arrived.  They were programs written mostly in C,C++ that run on a web server and used to build web pages.
As in the figure below, a client sends a request to web server, server forwards that request to a servlet, servlet generates dynamic content, mostly in the form of HTML pages, and returns it back to the server, which sends it back to the client. Hence we can say that servlet is extending the functionality of the webserver.

For making servlets, we need below s/w:
  • J2SE
  • Additional J2EE based libraries for servlets such as servlet-api.jar and jsp-api.jar.  Since these libraries are not part of J2SE.
  • A capable servlet web engine (webserver).
Apache Tomcat
==========================
Apache Tomcat is an open source web server, which is used as an official reference implementation of Java Servlets and JSP technologies. Tomcat is developed in an open and participatory environment and released under the Apache software license.
Environment Setup
  • From the http://tomcat.apache.org, download the zip file for the current release (any latest version) on C:\ drive and extract.
  • Create a new environment variable JAVA_HOME and set its value to the installation directory of JDK (e.g.: C:\Program Files\Java\jdk1.6.0_17).
  • Create another CATALINA_HOME environment variable. Its value should be the path till top-level Tomcat directory (C:\apache-tomcat-6.0.16).
  • Since servlets and JSP are not part of the J2SE, we have to identify servlet classes to the compiler. The server already knows about the servlet classes, but the compiler (i.e., javac) does not. So set CLASSPATH variable to C:\apache-tomcat-6.0.16\lib\servlet-api.jar;C:\apache-tomcat-6.0.16\lib\jsp-api.jar;.;
  • For testing this setup, from bin folder, run startup.bat file. Then open up a browser window and type http://localhost:8080. This should open the default page of Tomcat.
Standard Directory Structure of a J2EE Web Application
  • The  webapps folder is the top-level Tomcat directory that contains all the web applications deployed on the server.
  • Create a folder named  myapp in  C:\apache-tomcat-6.0.16\webapps folder. This name will also appear in the URL for application. For example http://localhost:8080/myapp/index.html. All JSP and html files will be kept in main application folder (myapp).
  • Create another folder inside myapp folder and change its name to WEB-INF. Configuration files such as web.xml will go in WEB-INF folder.
  • Create another folder inside WEB-INF folder and change its name to classes. Servlets and Java Beans will go in classes folder.
  • To test application hierarchy, make a simple html file e.g. index.html file. Write some basic HTML code into it and save it in main application directory i.e. webapps\myapp.
  • Restart the server and access it by using the URL http://localhost:8080/myapp/index.html
Servlet Types
==========================
Servlet related classes are included in two main packages javax.servlet and javax.servlet.http. Every servlet must implement the javax.servlet.Servlet interface; it contains servlet’s life cycle methods etc.
GenericServlet class
  • Available in javax.servlet package.
  • Implements javax.servlet.Servlet.
  • We need to extend our class from this class if we are interested in writing protocol independent servlets.
HttpServlet class
  • Available in javax.servlet.http package
  • Extends from GenericServlet class
  • Adds functionality for writing HTTP specific servlets as compared to GernericServlet
  • We need to extend our class from HttpServlet, if we want to write HTTP based servlets.
Servlet Class Hierarchy
==========================


Like all java classes GenericServlet also inherits from Object class. Apart from GenericServlet and HttpServlet classes, ServletRequest, HttpServletRequest, ServeltResponse  and HttpServletResponse are also helpful in writing a servlet. ServletRequest & ServletResponse are used in conjunction with GenericServlet.

HttpServletRequest & HttpServletRespose are used for processing HTTP protocol specific requests and generating HTTP specific response. Obviously these classes will be used in conjunction with  HttpServet class, which means you are making a HTTP protocol specific servlet.
GET & POST, HTTP request types
==========================
GET
  • Attribute-Value pair is attached with requested URL after ‘?’. For example if attribute is ‘name’ and  value is ‘abc’ then the request will be http://www.gmail.com/register?name=abc.
  • For HTTP based servlet, we have to override doGet() methods of HttpServlet class to handle these type of requests.
POST
  • Attribute-Value pair attached within the request body.
  • For this we have to override doPost() method of  HttpServlet  class to handle POST type requests.
Steps for making a Hello World Servlet
==========================
1) Create a directory structure for application (i.e. helloapp).

2) Create a HelloWorldServlet source file by extending this class from HttpServlet and overriding desired method. For example doGet() or doPost().

public class HelloWorldServlet extends HttpServlet {  
 public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
  throws ServletException, IOException {
   PrintWriter out = response.getWriter(); 
   out.println(“Hello World! ”);
 } // end doGet()
}


3) Compile  it and place the class file of HelloWorldServlet in the classes folder of web application.

4) Create a deployment descriptor (web.xml) and put it inside WEB-INF folder.

 

   
    HelloWorldServlet 
   HelloWorldServlet 
  
   
   HelloWorldServlet 
   /myfirstservlet      
  



5) Restart the server if already running access it using Web browser.

http://localhost:8080/myapp/myfirstservlet






No comments:

Post a Comment