SlideShare una empresa de Scribd logo
1 de 6
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 OK
       As 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, IOException
For 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 Santosh Dhoundiyal Graphic Era University

Santoshdhoundiyal008@gmail.com

Más contenido relacionado

La actualidad más candente

Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theoryMukesh Tekwani
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
Csajsp Chapter5
Csajsp Chapter5Csajsp Chapter5
Csajsp Chapter5Adil Jafri
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databasePayal Dungarwal
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods pptkamal kotecha
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletPayal Dungarwal
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servletvikram singh
 
SOAP, WSDL and UDDI
SOAP, WSDL and UDDISOAP, WSDL and UDDI
SOAP, WSDL and UDDIShahid Shaik
 
Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Peter R. Egli
 
Mulesoftmeetup4th july
Mulesoftmeetup4th julyMulesoftmeetup4th july
Mulesoftmeetup4th julyAnurag Dwivedi
 
Advance Java Programming( CM5I) 4. Networking Basics
Advance Java Programming( CM5I) 4. Networking BasicsAdvance Java Programming( CM5I) 4. Networking Basics
Advance Java Programming( CM5I) 4. Networking BasicsPayal Dungarwal
 

La actualidad más candente (20)

Java Networking
Java NetworkingJava Networking
Java Networking
 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
 
Servlets
ServletsServlets
Servlets
 
Java servlets
Java servletsJava servlets
Java servlets
 
Csajsp Chapter5
Csajsp Chapter5Csajsp Chapter5
Csajsp Chapter5
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
RMI (Remote Method Invocation)
RMI (Remote Method Invocation)RMI (Remote Method Invocation)
RMI (Remote Method Invocation)
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.Servlet
 
Servlets
ServletsServlets
Servlets
 
Request dispatching in servlet
Request dispatching in servletRequest dispatching in servlet
Request dispatching in servlet
 
Tutorial Solution
Tutorial SolutionTutorial Solution
Tutorial Solution
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
SOAP, WSDL and UDDI
SOAP, WSDL and UDDISOAP, WSDL and UDDI
SOAP, WSDL and UDDI
 
SERVIET
SERVIETSERVIET
SERVIET
 
java networking
 java networking java networking
java networking
 
Networking in java
Networking in javaNetworking in java
Networking in java
 
Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)
 
Mulesoftmeetup4th july
Mulesoftmeetup4th julyMulesoftmeetup4th july
Mulesoftmeetup4th july
 
Advance Java Programming( CM5I) 4. Networking Basics
Advance Java Programming( CM5I) 4. Networking BasicsAdvance Java Programming( CM5I) 4. Networking Basics
Advance Java Programming( CM5I) 4. Networking Basics
 

Similar a Servlet basics

Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1vikram singh
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technologyvikram singh
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesbharathiv53
 
Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01raviIITRoorkee
 
Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIPRIYADARSINISK
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache TomcatAuwal Amshi
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06Ankit Dubey
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servletssbd6985
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swiftTim Burks
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jspAnkit Minocha
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteTushar B Kute
 

Similar a Servlet basics (20)

Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
Servlets
ServletsServlets
Servlets
 
Http Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responsesHttp Server Programming in JAVA - Handling http requests and responses
Http Server Programming in JAVA - Handling http requests and responses
 
Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01
 
Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, API
 
Servlet & jsp
Servlet  &  jspServlet  &  jsp
Servlet & jsp
 
Servlet
Servlet Servlet
Servlet
 
Web container and Apache Tomcat
Web container and Apache TomcatWeb container and Apache Tomcat
Web container and Apache Tomcat
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Unit5 servlets
Unit5 servletsUnit5 servlets
Unit5 servlets
 
Networked APIs with swift
Networked APIs with swiftNetworked APIs with swift
Networked APIs with swift
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Marata
MarataMarata
Marata
 
gayathri.pptx
gayathri.pptxgayathri.pptx
gayathri.pptx
 

Servlet basics

  • 1. 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
  • 2. 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 OK As 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
  • 3. 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.
  • 4. 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>
  • 5. .... </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, IOException For 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.
  • 6. 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 Santosh Dhoundiyal Graphic Era University Santoshdhoundiyal008@gmail.com