Over View To HTTP Protocol

by Master Mind | 1:55 AM in | comments (1)

This tutorial gives the basic overview of HTTP protocol, if you are a web developer and already know the basics of the protocol, you can safely skip this tutorial. But if you are new to server side programming, you should read this tutorial carefully, as you will need to have basic understanding of HTTP protocol when you start learning Servlets and Java server pages.
The HTTP Protocol

Do you surf over the internet? If your answer is yes than you are already using HTTP protocol unknowingly. When you access any website using your favorite web browser, you are using HTTP protocol. Web browser uses the HTTP protocol to communicate with the web server.

HTTP stands for Hyper Text Transfer Protocol which is an application level protocol for transferring textual data between client and server. However HTTP can be used for many other tasks beyond its use for hypertext. HTTP is stateless protocol, that mean, client such as web browser sends the request to server, server responds and sends the requested data (such as a HTML document) or returns error code and closes the connection. Once the response is returned, server doesn’t remember anything about the client.
Http request methods and Headers

Whenever the client or browser sends the request to server for any resource such as a HTML document, it specifies the HTTP method, request URI, protocol version and optional Header information. After the client sends the request, server processes the request and sends the response. Response contains information such as status information, response headers and response data.

Following is an example of HTTP request which uses GET request method to ask for a HTML document named tutorial.html using HTTP protocol version 1.1

GET /tutorial.html HTTP/1.1

Following is the example of request header, which provides additional information about the request.

User-Agent: Mozilla/4.0 (compatible; MSIE 4.0; Windows 95) Accept: image/gif, image/jpeg, text/*, */*

Above header specifies the information about the client software and what MIME type the client accepts in response, using User-Agent and Accept headers.
Http request methods

The request method indicates the server what action to perform on the resource identified by the request-URI. HTTP/1.0 specifies three request methods: GET, POST and HEAD. HTTP/1.1 specifies five additional request methods: OPTIONS, PUT, TRACE, DELETE, and CONNECT. Servlets can process any of the above requests, however GET and POST methods meets most of the common requirements, so most of the time you will develop the Servlets which process theses two methods.
The GET request method

The GET request method is very simple and most frequently used method. The GET request is used for getting static information from the server such as HTML document, images, and CSS files. Although GET request is designed to retrieve static information, it can be used to retrieve dynamic information also by appending query string at the end of request URI.
Query String

Query string is used to pass additional request parameters along with the request. Query string consist of name value pairs and appended at the end of request URL with character ? Query string format

?name1=value1&name2=value&name3=value3 ….

Here the name is the name of the parameter and value is the value of the parameter that needs to be sent to the server for processing. name value pairs (or query parameters) are separated by ampersand sign (&). Following example show an HTTP URL which sends the request parameter userid in query string.

http://www.jsptube.com/login.jsp?userid=sudhir

The POST request method

The POST request method is commonly used to access dynamic resources. POST request is used to send the large amount of data to the server. POST request can be used to upload files to servers or even to send serializable Java objects or raw bytes. Unlike GET, POST request sends all the data as part of the HTTP request body, so it doesn’t appear in browser address bar and cannot be bookmarked. The data sent to the server is invisible to user. POST request is generally used to send HTML form parameters to server and to upload files.
Difference between GET and POST request methods

There are some differences between GET and POST requests. GET request sends the request parameters as query string appended at the end of the request URL, whereas POST request sends the request parameters as part of the HTTP request body. Since GET request sends the parameters as query string, parameters are displayed in browser address bar. User can bookmark the URL. Depending on how sensitive request parameters are, it may be a security issue. One another issue with GET request is, some servers limits the length of the URL to 240 characters, so if the too many parameters are appended in query string and URL exceeds this length, some web servers might not accept the request. These restrictions don’t apply to POST method as it sends the parameters as request body. POST requests cannot be bookmarked.
NOTE: HTTP/1.1 does not impose any restrictions on the length of URL.

Introduction To Web Containers

by Master Mind | 1:54 AM in | comments (1)

Overview of web containers:

Note: Servlets and JSP pages are collectively called web components.



This tutorial provides the basic introduction to J2EE web containers (Also called Servlet container or Servlet engine).

In J2EE architecture the basic purpose of the container is to provide the runtime environment for the components. components are managed by container, in order to be managed by container components must follow certain contract. J2EE specification defines contract between components and container, and specifies the deployment model for components. Contract specifies how components should be developed and deployed, and how components can access services provided by container. When developing applications with J2EE, we develop components that follow the contract defined in the specification.

In J2EE this contract is specified in term of various interfaces and classes, developer writes the classes that implements this interfaces or extends the classes and provides appropriate implementation of various methods and rest will be done by container. Container takes the responsibility of instantiating, initializing and invoking the components. Application does not directly instantiate or invokes components. As you will see that application never instantiate an object of servlet or call any of the init(), service() or destroy() life cycle method, all of these is handled by web container. Web container automatically instantiates and initializes the Servlets on application startup or when invoked for the first time. Container calls the service() method when user requests the servlet.

The web container is a Java runtime environment which is responsible for managing life cycle of JSP pages and Servlets. A web container is responsible for instantiating, initializing and invoking Servlets and JSP pages. The web container implements Servlet and JSP API and provide infrastructure for deploying and managing web components. The web container is part of web server or application server that provides the network services over which request and response are sent. A Web container is may be built into the web server or it may be installed as an additional component to a web server. As per the specification, all web containers must support HTTP protocol however it may support addition protocols like HTTPS.

Web container is also called servlet container or servlet engine. We will use Tomcat throughout all the tutorials. Tomcat is an open source web server, which is the web container reference implementation. You can find more information about tomcat at http://tomcat.apache.org/. If you have not already downloaded and installed Tomcat, read this tutorial Setting up a Servlet development environment which explains how to install tomcat and setup the environment. We will need to setup the development environment to run the various examples and code sameples given throughout the tutorials.

Understanding The Java Structure

by Master Mind | 1:53 AM in | comments (0)

Web Applications use a standard directory structure defined in the Servlet specification. When developing web applications on J2EE platform, you must follow this structure so that application can be deployed in any J2EE compliant web server.

A web application has the following directory structure @TODO INSERT IMAGE OF DIRECTORY STRUCTURE

The root directory of the application is called the document root. Root directory is mapped to the context path. Root directory contains a directory named WEB-INF. Anything under the root directory excepting the WEB-INF directory is publically available, and can be accessed by URL from browser. WEB-INF directory is a private area of the web application, any files under WEB-INF directory cannot be accessed directly from browser by specifying the URL like http://somesite/WEB-INF/someresource.html. Web container will not serve the content of this directory. However the content of the WEB-INF directory is accessible by the classes within the application. So if there are any resources like JSPs or HTML document that you don’t wish to be accessible directly from web browser, you should place it under WEB-INF directory.
WEB-INF directory contains

* WEB-INF/web.xml deployment descriptor
* WEB-INF/classes directory
* WEB-INF/lib directory

The classes directory is used to store compiled servlet and other classes of the application. lib directory is used to store the jar files. If application has any bundled jar files, or if application uses any third party libraries such as log4j which is packaged in jar file, than these jar files should be placed in lib directory. All unpacked classes and resources in the /WEB-INF/classes directory, plus classes and resources in JAR files under the /WEB-INF/lib directory, are made visible to the containing web application.
Note: directory and file names are case sensitive.

Introduction to Java Servlets

by Master Mind | 1:53 AM in | comments (0)

Introduction to Java Servlets

Now you have the basic understanding of the HTTP protocol, web containers, and J2EE web application structure. Before you start learning Servlet API, this tutorial provides the basic understanding of the Java Servlets.
What is a servlet?

Servlets are Java classes that process the request dynamically and generate response independent of the protocol. Servlets are defined in Java Servlet API specification. At the time of writing this tutorial, the latest Servlet API specification is 2.5 available here .

Servlets are server side Java programs which extends the functionality of web server. Servlet are protocol independent that means it can be used virtually with any protocol to process the request and generate the response. However in practice Servlets are used to process the HTTP requests and generate the HTML response.

I don’t know if the implementation is available for any other protocol like FTP or SMTP! Let me know if you know about the Servlet implementation for any other protocol.

If you are not in web development already and not aware of server side programming languages, a question which may arise in your mind is: if the Servlets generates the HTML than why do we need the Servlets? We can simply create the HTML pages instead and deploy them in any web server. The answer is in the term dynamic request handling. If you create the HTML pages which are directly served by web server, it is not a dynamic web application. A HTML document is not dynamic. It is a simple static content which is served as it is whenever a request is received. Servlets are used to generate the response dynamically based on the user request. The dynamic response generation is achieved by embedding the application logic in HTTP request-response process.

The initial goal of the HTTP protocol was to enable the sharing of information over the internet. HTTP defines what makes a valid request and valid response, how clients can request information and how server can send response, but it does not define how the response could be generated. That means HTTP protocol does not define any way to incorporate the application logic in response generation. Here the server-side technologies like PHP, ASP, Java Servlets and Java server pages come into picture. These technologies provide the way to write the application logic which sits in between the request handling and response generation phases and generate the response dynamically based on the information present in the request.

Let’s see how it is achieved with Servlets:

Whenever the web container receives a HTTP request for a resource, based on the URL of the incoming request, web container determines if the request should be handled by a servlet. If the request is to be handled by a servlet, the container checks to see if the instance of the servlet is already available. If an instance is available, the container delegates the request to that instance. If no instance of the servlet is available, container creates a new instance of the class of servlet and delegates the request to that newly created instance.

When delegating the request to a servlet for processing, web container creates the objects of class HttpServletRequest and HttpServletResponse which represents the HTTP request and HTTP response and passes these objects to the Servlet instance. The instance of HttpServletRequest provides servlet the access to incoming HTTP request information, like HTTP headers, form parameters, query string etc. The instance of HttpServletResponse provides the way to send the response to the client. It provides the methods to write the HTML to the output stream to the client, add response headers, add cookies etc.

During the request processing, the code within the servlet reads the request information, executes the application logic, and generates the response. Once the processing has been completed the response is returned to the client by writing it to the HttpServletResponse.

This tutorial provides step by step instructions to setup the development environment required to test various servlet examples given throughout all the tutorials. This tutorial will also be helpful when you start practicing servlets yourself.

List of required softwares:
# JAVA 1.5 or 1.6
# Tomcat 5.5.16
# eclipse 3.3

Note: this tutorial explains how to install required software on your windows based PC only.



First of all you need the Java software development kit 1.5 or 1.6 (JAVA SDK) installed.
Checking if JAVA is already installed in your system

Follow this step to check if Java software development kit (JDK) is already installed in your system.

To determine if JAVA SDK is already installed in your system, run the following command from command prompt.

Java –version

If JAVA platform is already installed, it will display the version of the JAVA SDK. Below screen shows JAVA version 1.6 running on a windows system. If command executes successfully and shows JAVA version 1.6, you can skip the next step.otherwise download and install the Java SDK as explained in next step.




Installing JAVA

First thing you need to install is Java software development kit (Java SDK) .Download the Java development kit 6 (JDK 6) from Sun Java SE Download site. Current latest version is JDK 6 update 6.
Setting up the JAVA_HOME

Once the JAVA SDK is installed follow this step to set the JAVA_HOME environment variable. If JAVA_HOME variable is already set, you can skip this step.

Right click on My Computer icon on your desktop it will open a popup menu, click on properties, it will open the system properties window, then click on advanced tab.

Installing JAVA

First thing you need to install is Java software development kit (Java SDK) .Download the Java development kit 6 (JDK 6) from Sun Java SE Download site. Current latest version is JDK 6 update 6.
Setting up the JAVA_HOME

Once the JAVA SDK is installed follow this step to set the JAVA_HOME environment variable. If JAVA_HOME variable is already set, you can skip this step.

Right click on My Computer icon on your desktop it will open a popup menu, click on properties, it will open the system properties window, then click on advanced tab.



Click on environment variables button, it will open the environment variables window as shown in figure below. Initially there will be no JAVA_HOME environment variable set as shown in figure.



Click on new button to add new system variable JAVA_HOME.




Installing Tomcat

Tomcat is an opensource web container. it is also web container reference implementation. You will need tomcat 5.5.16 installed on your system to test various servlet and JSP examples given in other tutorials.

Download the latest version of tomcat from this URL . Download the jakarta-tomcat-5.0.28.tar.gz and extract it to the directory of your choice.

Note: This directory will be referred to as in other tutorials.

That’s all, tomcat is installed. It’s very easy, isn’t it?
Starting and shutting down Tomcat

To start the tomcat server, open the command prompt, change the directory to TOMCAT HOME/bin and run the startup.bat file. It will start the server.

>startup



To shut down the tomcat server, run the shutdown.bat file. It will stop the server.

>shutdown


Verifying Tomcat installation

To verify that tomcat is installed properly, start the server as explained above, open the web browser and access the following URL.

http://localhost:8080/index.jsp

It should show the tomcat welcome page, if tomcat is installed properly and server is running.


Setting up the CLASSPATH

Now you need to create a new environment variable CLASSPATH if it is not already set. We need to add the servlet-api.jar into the CLASSPATH to compile the Servlets. Follow the same steps as you did to create the JAVA_HOME variable. Create a new variable CLASSPATH under system variables. Add /lib/servlet-api.jar in variable value field.