Showing posts with label dynamic response. Show all posts
Showing posts with label dynamic response. Show all posts

Monday, January 27, 2014

Web Application Artifacts in Servlet Technology

When we develop web applications in java servlet technology we need some of artifacts.

Before read this article please go through following article which will give more information about web application technologies in java


We need following artifacts. These are all very important to build servlet application in java
  1. Web application Directory Structure
  2. Deployment Configuration File
  3. Servlet Java Class
  4. Servlet-api.jar file
  5. Servlet Container Provider Servers (Tomcat)

Web application Directory Structure

In any web application java that should have some particular directory structure. Once we done all development we need package as .war file.

What is war (Web Archive)?

A Web Archive is web application standard package that represent the web application. A war can be understood by any web container/server. We need to package ad war and deploy into servers.

We have java commands to package web application as war file. We can also make war file from eclipse IDE tool.

The following is example web application directory structure.




The following is more about web application directory structure


Deployment Configuration File

Deployment configuration provides the full information about application and its resources. This information we will provide through the web.xml file and it have many predefined tags all tags information provided in DTD file.

Coming to servlet web application on web.xml we need to configure the servlet. Container will use this information and execute the appropriate servlet when client is requested for dynamic response through simple http URL notation.

The following is full details of tags in web.xml


The following is simple servlet information in web.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <servlet>
    <servlet-name> helloWorldServlet </servlet-name>
    <servlet-class>com.liferaysavvy.meera.HelloWorldServlet</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>helloWorldServlet </servlet-name>
    <url-pattern>/helloworldservlet </url-pattern>
 </servlet-mapping>
</web-app>

Servlet

Servlet is parent tag here servlet definition will start. This tag has two child tags servlet name and servlet class.

Servlet name:

 Servlet name is simple human understandable name which is reference name to servlet class.

Servlet class:

Servlet class is simple java class and it contains the servlet behavior

Servlet mapping

Servlet mapping is simple URL information to servlet container which helps to the container when client request for dynamic request. From the URL it will identify which servlet to be invoke/execute

Servlet mapping tag will map the servlet with some url pattern. This tag has two child tags i.e. Servlet Name and url-pattern

Servlet name we already know

url-pattern

url pattern tag will map the servlet with simple http url pattern. This url pattern will be used by client to invoke servlet.

Note:

When we map servlet class with URL pattern we need to use servlet name as common tag.

Servlet Java Class

Servlet java class is simple java class which should implements the java servlet interface. Servlet interface is simple java interface have certain abstract methods. To get servlet behavior to java class we need to implement servlet interface.

Servlet API implementation provides some servlet implementation classes. We will use those classes and interfaces to get servlet behavior to our class.

Generally we need to implement servlet interface to our java class but in the implementation we will use another java class that already implemented servlet interface.

The following is servlet implementation hierarchy



Note:

We will use Http Servlet and it will use http protocol to invoke the servlet from client. We can also implement servlet for other protocols and as of now we will have http protocol based servlet implementation is available.

Servlet API implementation packaged as servlet-api.jar file which is from sun micro system implementation.

Servlet-api.jar file

Servlet-api jar is set of java classes and interfaces implemented by sun micro system. This is one of the servlet API implementation.

If we develop any application we need this jar file. We will use many classes and interface from this jar file.This jar should be available in application class path or server global class path

What is class path?

Class path is a place where all java classes and interfaces will be available.We will use following things as class path
  1. Application WEB-INF/classes directory
  2. Application WEB-INF/lib directory
  3. Server lib directory

Application WEB-INF/classes directory

Every web application have WEB-INF/classes directory where all java compiled classes will be available. This directory is only specific to respective web application. If any class available in WEB-INF/classes directory will be loaded by server in the JVM memory as soon as application is deployed in server.

Application WEB-INF/lib directory

WEB-INF/lib another class path for server here all required jar files will be placed. The jars which are available in WEB-INF/lib directory will be used by respective application. These are application specific jar files.

Server lib directory

Server lib directory is called global class path this directory shared by all web application which are deployed in server. Any jar file or java class available in server lib then it is available to all applications.

Note:

Server global class path or global lib will be varied based on server.

If any jar file available in server global class path that particular jar should not be available in application class path if such cases we will get class conflicts.

For tomcat it will be tomcat-7.0.40\lib this is default directory for tomcat and we can also change this by configure the details in catalina.properties.

Application Class path priority


WEB-INF/classes > WEB-INF/lib > Server lib directory


Servlet Container Provider Servers (Tomcat)

Servlet container is required to run servlet. This container is responsible for manage the life cycle of servlet.

Servlet container can be provided by different server vendors by default. So we can run servlet application in any server if the server have servlet container.

Tomcat is one of servlet which provide the servlet container. We will use tomcat server to run servlet based web applications.

Servlet Execution Flow
  • We already know servlet information is available in web.xml file.
  • When the application is deployed in the server then servlet container is responsible to read all servlets information from web.xml file
  • In web.xml file servlet name and its URL mapping will be available. Servlet container will create servlet mapping objects in the server memory that object contains servlet URL pattern and its respective servlet class.
  • When client is requested through http URL and the URL will have servlet mapping pattern.
  • As soon as server receives the request from client then server will delegate the request to servlet container because the request is dynamic request.
  • As soon as request received by servlet container then it will check with servlet mapping objects in the server memory.
  • If any mapping object find for client request then it will get the respective servlet class information and it will create the object for servlet and it will execute the appropriate methods on behalf of servlet object.
  • These servlet methods are responsible to prepare dynamic response in the form of html or other browser understandable format.
  • Once response preparation is completed then servlet container collect the response data from servlet then the container give the response back to server.
  • As soon as server gets the response from container then it will give to client then client will display response in browser so that it is able to viewed user.

Request for servlet/Call servlet:

In web application we need to request to the servlet in particular URL pattern

The following is generic URL request to servlet


http://<hostName>:<port Number>/<application context>/<servlet url pattern>

Example:

http://localhost:8080/myapp/helloWorl


Host Name:

Host name is name of computer or domain name where server is running.

If we run in server in local computer then it is localost/127.0.0.1

If server is running in local area net work or in the internet then we will have domoan name or IP address

Example:

198.168.34.23

Application Context:

Application context is name of our web application name. this is our application start point.
We can see the application context name in server deployments directory in general application name itself is application context.

Example:  MyApp

Note:

Application Name/Context name should not have spaces or special characters.

Servlet URL pattern:

Servlet URL pattern is simple string which will used in client requested URL. We need to append servlet URL pattern to client request so that servlet container can identify the servlet and it will be executed. Servlet URL pattern we can see in web.xml file.

Example configuration in web.xml

<servlet-mapping>
    <servlet-name>helloWorldServlet </servlet-name>
    <url-pattern>/helloworldservlet </url-pattern>
 </servlet-mapping>


Related Articles


Reference links for Servlet Technology and its Development


Author

Dynamic Web Application Technologies in Java

We are all very familiar with dynamic web applications. Web application is serve the response based on user request and it can give the dynamic response.

The following is web application introduction please go through it first.


Different technologies are in the world to develop dynamic web applications. We have many technologies from different languages.

The following are popular dynamic web application development languages.


Java, PHP, .NET, Python, Ruby and Perl


Now we will talk about java language. Our objective is to develop web application in java language.

What is java?

Java is object oriented programming language which has many libraries to develop many applications.

In java we can categorize application in two types

Standalone Application

Web based applications

Standalone Application

Stand alone application are kind of application which have its own client and server. This application should have their independent environment that should run applications. In this application have their own protocol to communicating client and server.

If we develop any application as standalone in each machine we should install client application that will connect with server.

This application we can develop in Java by using Swings.

Example:

Super market Billing application and Hotel billing applications

Web Based Applications

Web base application also need client i.e. browser. When we develop any web based application we need not to have any special client and it will run in web browsers. These applications will work request and response based mechanism using Http protocol.

We already know web application required web browser and browser can understand only HTML and Java Script language.

When we talking about web application that need following artifacts
  1. Client/Browser
  2. Server
  3. Server Side Components
  4. Containers

Client/Browser

A client is program that able to send request t the server and able to view to end user. In web application browser is client. Browser knows only html and java script. So server response should be browser understandable language.

Server:

Server is a program that will manage resources and that will provide environment to process client request. Server can communicate by using http protocol with client.
When client send request server will generate response and that response should understand by client i.e. HTML, images and files.

Server Side Components:

Server side components are special kind of programs that will prepare the dynamic response when the client is requested.

Server only can serve the response but it can’t prepare the response that is why we need some special program that will prepare response.

Here server will provide environment to run server side components with help of other programs that is called containers.

Containers

Containers is also like server but its have some specialize characters and it will provide platform to run server side components to prepare dynamic response. It will manage the life cycle of components

Java Server Side Components

To implement server side components we need some technology. Java also has server side technology to develop dynamic web application that is called Servlet.

If we want develop any dynamic web application we need server side components that can generate the dynamic response. Java has server side technology to develop dynamic web application using servlet.

What is servlet?

Servlet is server side component which will prepare the dynamic response when client is request.

Java people have given some set of rules when we implement servlet technology this rules are called API.

What is API?

Application Programming Interface (API) is set of rules and specification for technology.
So Java People have given set of rules and specification to servlet so that any vendor to implement servlet that should meet the API specifications.

We have many specifications in java like servlet specification is one to develop web applications

Anyone can implement API specification such people we will call it as Vendors.

A vendor is a company or person who can follow the API specifications and develop the software is like implementation provider.


Some implementations are Enterprise and some are Open Source.

How does servlet work?

To work or run servlet we need servlet container that is responsible to run servlet when client is request for response.

Servlet container will come with many servers so that many servers can run servlet.

The following are the popular servers in the market which can provide run time environment to servlet
.

Tomcat, JBoss, GlashFish, Jetty, Webspear, Weblogic and Pramathi


The following screen to show servlet work flow.



Servlet execution flow
  • Client will send the http request to the server and server will identify the request type.
  • If the request type is static then server itself processes the request and server the response.
  • If the request type is dynamic then server will delegate the request to servlet container. Now servlet container will find the name servlet to be executed from the request.
  • Container finds the servlet then it will execute the appropriate servlet and servlet will prepare the dynamic response.
  • Now container give the response back to web server then web server serve the response to the client.

How does container will identify which servlet to be executed?

As soon as we deploy the application in the server then servlet container will load all servlet information in the memory. All servlet information will be places in Web application deployment configuration file.

What is deployment configuration file?

Deployment configuration file is simple xml file and it is heart of web applications.

Deployment configuration file having set of predefines tags which will provide the servlet information to container and other web application information.

Deployment configuration file have information like servlet URL mapping, servlet name and servlet implementation java class.

This deployment configuration we will specify as web.xml file. Any web application in java should have web.xml file.

Author

Recent Posts

Recent Posts Widget

Popular Posts