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
- Web application Directory Structure
- Deployment Configuration File
- Servlet Java Class
- Servlet-api.jar file
- 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 namewe 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 jaris 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
- Application WEB-INF/classes directory
- Application WEB-INF/lib directory
- 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 libdirectory
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
0 comments:
Post a Comment