HTTP Basics, Web Server, Servlet Container and the Java Servlet API
This article tries to demystify HTTP, "servlet", "web server", "application server", "servlet container" and gives the fundamentals of the Java Servlet API (that comes with the J2EE SDK).
Introduction to HTTP
The ABC of HTTP
HTTP (Hyper Text Transfer Protocol) is one of the numerous protocols, which computers use, to talk to each other (just the same way two human beings need a common language to communicate). When I say 'talk to', I mean, exchanging data. In the client-server world, a client is the machine that makes the first call to the server (thinking of the word "accost", which means 'to approach and to speak first'). A machine can be both client and server.
When a computer initiates a connection to another computer, in HTTP jargon, it is a request. When the server computer sends data back, it is a response. An HTTP server accepts request from any client and always sends a response. Once a response is sent, the server does not retain information about the request. Neither does it retain any information about the client. That is why HTTP is a stateless protocol.
HTTP Requests
An HTTP request has three parts: (a) a request line, (b) one or more headers and (c) a message. The message is optional.
- (a) Request Line: A request looks like:
GET /science /light.html HTTP/1.1
'GET' is the name of the method. In HTTP 1.0, other methods are: HEAD and POST. In HTTP 1.1, additional methods available are: PUT, OPTIONS, DELETE, TRACE, CONNECT. The second token of the message, "/science/light.html", is a URI. URI stands for Universal Resource Identifier. URI gives information about the location of the resource to be gotten. The last token is the version of HTTP to be used. The current version is 1.1. 'GET' method is used to retrieve a resource identified in the URI. The 'POST' method, on the other hand, is used to send data to the server. 'GET' can also send data to the server, but is limited to 255 characters, and is in the form of name-value pairs, separated by ampersand. An example of sending data to the server using GET is:
GET /science /light.html?user=john&pwd=abcd HTTP/1.1
POST method is used to send HTML FORM values to the server. GET and POST are the common methods used.
(b) Headers:Headers contain meta-information. 'Meta-information' means information about information. They include information like: what is the type of data (character, binary etc.), what is the size of data (in Kilobytes).
(c) Message: Message body is optional. POST has a body with all the information from the HTML FORM. GET does not have one. If a message body exists, it is preceded by a blank line.
HTTP Responses
A HTTP Response, the same way, has a (a) response line, (b) one or more headers, and (c) a message.
- (a) Response Line: A response line looks like:
HTTP/1.0 200 OKAs you might have guessed, the first token is the HTTP version. The second token is one of the many predefined status codes, and the last one is an English description of the code.
(b) and (c) share the same characteristics as that of as a HTTP Request.
Behind buzzwords:
Web Server
Web Server is a machine that has a HTTPD service running. A web server is the one that handles HTTP requests and generates HTTP responses.
Servlet
A Servlet is a server-side entity for servicing HTTP requests.
Servlet Container
A Servlet Container is a sub-set of a Web Server. A Servlet Container is a separate module; it may run within the web server as a single program (called Standalone, in this case), may run as a different program, but part of the same address space (In-Process), or run in different process-spaces. Tomcat is a very popular standalone servlet container, and can be acquired from http://jakarta.apache.org.
Web Application
A web application is an application that is accessible from the web. In that sense, all servers working towards the purpose of running a web application is an application server.
Application Server
Tomcat is an application server, in that sense, if that is the only server used in an application. An application may use other servers, like BEA WebLogic or IBM WebSphere. These application servers provide additional services like an EJB Server, a JMS Server, a JNDI Naming Server, etc.
The world of Java Servlets
Sun provides a set of specifications, which dictate how a servlet would communicate with a web server. Tomcat is a servlet container that conforms to those specifications. So, when application developers use the Servlet API, we are happy campers, because Tomcat has implemented the Java interfaces we are going to use. API stands for Application Programming Interface. It is a set of classes and interfaces for us to use. Servlet API is a part of Sun's J2EE SDK, that can be downloaded from http://java.sun.com/j2ee.
Servlet API
javax.servlet.Servlet (interface)
This is the numero uno interface of the entire API. Any Servlet class implements this interface. The five methods to implement are: init(), service(), destroy(), getServletConfig() and getServletInfo()
javax.servlet.GenericServlet (abstract class)
Abstract class, implements the java.servlet.Servlet interface, provides implementation of all methods except the service method.
javax.servlet.ServletRequest (interface)
This interface abstracts a Request.
javax.servlet.ServletResponse (interface)
A generic interface for abstracting a response.
javax.servlet.http.HttpServlet (abstract class)
Extends GenericServlet. Has a new method:
protected void service (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException;
javax.servlet.http.HttpServletRequest (interface)
This interface extends ServletRequest, and gives HTTP-specific functionality to retrieve information about request headers, request parameters, request attributes.
javax.servlet.http.HttpServletResponse (interface)
Extends ServletResponse. Provides abstraction for a HTTP response. Methods to set header types, content types and response body exists.
javax.servlet.ServletContext (interface)
This is an important interface to understand. It provides a rendezvous for all servlets, JSP's of a web application to share common knowledge. There is one and only ServletContext for an application. Methods to look for are:
java.netURL getResource(String relativePath)
java.io.InputStream getResourceAsStream(String relativePath)
javax.servlet.ServletConfig (interface)
ServletConfig helps us get the values of initialization parameters as declared in the deployment descriptor file. A "deployment descriptor" is a XML document, which can be used to declare initial parameters that the application can use at run-time. The deployment descriptor is named "web.xml". A snippet of web.xml with servlet initialization parameters:
<web-app> <servlet> <servlet-name>HumbleServlet</servlet-name> <servlet-class>com.abcd.HumbleServlet</servlet-class><init-param>
<param-name>jdbcDriverName</param-name>
<param-value>com.abc.Type4.ThinDriver</param-value>
</init-param><init-param>
<param-name>parameterOne</param-name>
<param-value>23</param-value>
</init-param></servlet>
....
</web-app>
Now it is easy to get the parameter value by calling getInitParameter (String paramName), which returns a String. Database driver names, classes, urls's are good candidates for entry.
java.servlet.http.HttpSession (interface)
Servlet container implements this interface. A representation of a session. The container takes care of associating the right session for a user.
javax.servlet.RequestDispatcher (interface)
Interface to pass ServletRequest, ServletResponse objects between servlets. From a servlet, one can use it to forward control to a different servlet, a JSP, a HTML or any resource using the method:
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOExceptionFor including contents of a different resource, one can use:
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException
Accessing a database from a Servlet
It is very easy to access a database from a Servlet. If using JDBC, then one can instantiate a driver class and get hold of a ?connection? object. The driver class and connection interface is a class provided by Sun in its software development kit (SDK, for short). Both J2SE and J2EE come with their driver classes.
Connection to the database can be configured in the deployment descriptor file. Another trick would be to get the connection in the init(..) method. This way, the same connection can be re-used. Care must be taken to use connection objects, because a connection to a database is expensive, and can slow your application down.
If you chose to use a persistence layer instead of JDBC, like Object Relational Bridge from Apache Software Foundation, or iBatis, then you need not worry about writing code to access database. Necessary configuration files (XML documents) take care of them.
Another thing to keep in mind is servlets are not thread-safe (i.e. multiple threads can access the service(...) method.
Summary
With clearing some of the roadblocks that a "Servlet" newbie might encounter, I am hoping that after reading this article, she or he might be able to go a little deeper into the subject.
Please e-mail if there is anything you want to see here, in the Java Web technology domain.
Prepared by Saikat Goswami, Boston, Massachussets, sai_nyc@hotmail.com
- Saikat Goswami's blog
- Log in to post comments
Comments
One thing I forgot to Mention
1. Session Binding Listners
2. Filters
If you add this in any of your next articles, it would be great
Raghubir,
(1) Comparing GET and POST has been covered.
(2) http://www.w3c.org is the official site for the W3C Consortium. You can get detailed information on request headers, etc.
(3) Deployment has been covered.Please read the info on javax.servlet.ServletConfig
(4) Will keep this in mind.
(5) A book which has diagrams of servlet life-cycle is by Manning Publishers, "SCWCD Certification", by Hanumant Deshmukh, et al.
(6)Will try to add more code.
(7) Will write on design patterns. Stay tuned !!
Best Wishes,
Sai
Hi,
Nice article. Can you explain the SingleThread Modle and Marker Interface with simple examples?
Thank you in advance.
Regards,
Mohan G Ram
helped me a lot in understanding how Java works with HTTP..good job..expecting more
If you could demistify sockets it would be great.
Because underlying everything is socket.
I would like to know what exactly socket is?
I found the following points that can be put into the DOC so that it becomes more complete
1. Limitation of GET and POST requests
2. If you give a link to the common HTTP request headers
3. Should have told something about deployment
and WEB-INF
4. Espected something to be told on driver classes and connting to oracle via classes12.zip
5. Still again no diagrams to explicitly explain the servlet lifecycle
6. Sample code to demostrate the power of servlet and JSP.
Since you are writing articles on J2EE can you also write something about design patters of the web tiers using servlet and JSP(I am talking not only on Model 1 and Model 2 , but also which patterns are more fit to be put in the UI level eg Decorative filter and front controller and stuffs like that)
Anyways ..great work ...expecting better from you ...
Chalao .....
Going through this extensively, thanks for simplifying quite a lot of things. i think more of these definitions are required for non developers like us
This is very good article for people to know the basic of http.
I hope we will get more article of this nature in future.
The topics is discussed very nicely. One can understand it very easily.
hi sivakumar,
I think you are talking about connection pooling, which is configured separately. A servlet can hold as many connections as you will ask it to.
My article for this month is on "Connection Pooling". It will be published in a few days.
Thanks,
Sai
hi,
Nice article to see.. I just wanna know,how many concurrent connections are made by l90servlet? is there any restriction on this? I want l90servlet to start morethan 50+ ifweb90 processes, but its not starting..I want those connections to be connected to oracle9ias.
thanks in advance for your suggestion..