Nearby lessons
29 of 34Servlet - Examples: Directory Structure & web.xml
- See complete runnable servlet programs
- Understand the output of each program
- Copy and deploy programs in Tomcat
Web application directory structure, web.xml configuration, init parameters, welcome files, and error pages.
UNIT-2: Directory Structure and Deployment of Web Application
Objective:
Construct File and Directory Structure of web application that may contain
- Static Content
- JSP Pages
- Servlet classes
- Deployment Descriptor
- Tag Libraries
- Jar Files
- Java Class Files
advapps2A
| Name of Application *.html | Static | Publicly |
|---|
OR *.jpg Context Accessable
*.css Resources
Context Root
:::
:::
*.jsp
WEB-INF
web.xml
classes
*.class
lib
*.jar
tags
*.tld
*.tag
Servlet Specification defines a standard structure for web applications so that every web server
can provide support for that structure irrespective of vendor.
Total static information we have to place within the context root directly. All resources present in
context root are publicly accessable resources. ie any person can access these resouces directly by
its name.Hence we cannot place secured resouces within the context root directly.
http://localhost:7777/advapps1C/login.html
For every web application we have to maintain WEB-INF Folder. All the secured resources we have
to place in this folder and these are not publicly accessable resources. If any person trying to
access these resouces directly by its name then we will get 404 status code.
http://localhost:7777/advapps1B/WEB-INF/web.xml ==>404
For every web application, we have to maintain one special xml file named with web.xml. It is also
known as Deployment Descriptor. Web container will use this web.xml to get our servlets
information.Hence this web.xml acts as GUIDE to web container.
We have to place this web.xml in WEB-INF folder directly.
Within the WEB-INF, we have to create classes folder to place all .class files.(Both Servlet classes
and normal java classes)
Within the WEB-INF, we have to maintain lib folder to place all required .jar files(like
ojdbc6.jar,log4j.jar etc)
Note:
if we place our class files in classes folder and lib folder, we are not required to set classpath
explicitly, b'z web container always searches in these locations by default.
Note:
For the required .class file web container will search first in classes folder and if it is not available
then only it will search in lib folder.i.e classes present in classes folder will get more priority than
classes present in lib folder.
All Tag libraries we have to place inside WEB-INF either directly or indirectly.
| Resourse | Location |
|---|---|
| 1) Static Context | 1) Within the Context Root directly |
| 2) JSP Pages | 2) Within the Context Root directly. If JSP is |
secured then we have to place inside WEB-INF
UNIT-2: Directory Structure and Deployment of Web Application
Q. In how many places we can place our servlet in web application?
There are 3 places
UNIT-2: Directory Structure and Deployment of Web Application
Deployment Descriptor: (web.xml)
Objective:
Describe the purpose and syntax of the following deployment descriptor elements?
1.<error-page>
2.<init-param>
3.<mime-mapping>
4.<servlet>
5.<servlet-mapping>
6.<servlet-class>
7.<servlet-name>
8.<welcome-file>
Deployment Descriptor(DD) is an XML File named with web.xml,should be placed in WEB-INF
directly.
Web container uses this web.xml to get web application deployment information. i.e web.xml acts
as GUIDE to web container.
web.xml provides declarative mechanism for customizing our web application without touching
the source code(with the help of initialization parameters).
For every web application we have to maintain exactly one web.xml and should be placed in WEB-
INF folder directly.
Anatomy of web.xml:
UNIT-2: Directory Structure and Deployment of Web Application
until servlet 2.3 version,web.xml is validated by using DTDs where order is important.Hence until
Servlet 2.3V the order of these top level tags is important.
But from Servlet 2.4V onwards web.xml is validated by using Schemas where order is not
important.Hence from servlet 2.4 Version onwards we can take these top level tags in any order.
Among these 27 top level tags , no tag is mandatory.Hence the following are valid web.xml files
| <web-app> | OR <web-app/> |
|---|
</web-app>
1.<servlet>:
We can use <servlet> tag to declare servlets in web.xml.
<servlet> tag contains the following 9 child tags...
UNIT-2: Directory Structure and Deployment of Web Application
The order of these 9 tags is important and we should not change.
Among these 9 tags the following 2 tags are mandatory.
<servlet-name>
<servlet-class> or <jsp-file>
<servlet-name>:
According to Servlet specification there are 3 names are possible for every servlet.
UNIT-2: Directory Structure and Deployment of Web Application
- Original Name OR Developer's Name specified by <servlet-class>
- Deployer's Name OR Logical Name specified by <servlet-name> tag
- End User's Name speficied by <url-pattern>
The main advantages of Logical Name are:
- We can achieve security because we are not highlighting our internal naming conventions to the
end user.
2.Without effecting end user we can change our internal naming conventions.
3.We can map same servlet with different url-patterns
Note:
Within web.xml Logical Name is always unique.
Within the servlet we can get Logical Name by using getServletName() method of ServletConfig
interface.
UNIT-2: Directory Structure and Deployment of Web Application
Note:
All methods of ServletConfig interface are by default available in our servlet.Hence within our
servlet we can call these methods directly.
<servlet-class>:
By using this tag,we can specify the original name of servlet(Developer's name)
Web container will create object for this class only.Hence compulsary it should be concrete class
but not abstract class or interface.
Demo Program
|--WEB-INF
|--date.jsp
|--web.xml
http://localhost:7777/advapps2B/test==>valid
http://localhost:7777/advapps2B/WEB-INF/date.jsp==>404
Servlet Initialization Parameters(<init-param>):
If the value of the variable will change frequently then those values are not recommended to
hard-code within the servlet class.
The problem in this approach is, If there is any change in the value, to reflect that change,we have
to recompile,rebuild and redeploy application, and sometimes even server restart also
required,which creates big business impact to the client.
Such type of variables we have to configure in web.xml by using <init-param> tag.
The advantage in this approach is if there is any change in the value, to reflect that change just
redeployment is enough,which won't create big business impact to the client.
We can declare servlet initialization parameters in web.xml as follows...
Demo Program
We can declare any number of init parameters but for each parameter one <init-param> tag.
Within the servlet we can access servlet initialization parameters by using ServletConfig object.
ServletConfig interface defines the following methods to access these parameters inside servlet.
Demo Program
Returns the value associated with specified parameter.
Returns null if the specified paramter is not avaialble.
Demo Program
Returns all initialization parameter names.
If the specified servlet does not contain any initialization parameters then this method returns
empty enumeration object but not null.
GenericServlet implements ServletConfig interface and hence GenericServlet provides
implementation for the above 2 methods.Within our servlet we can call these methods directly.
Demo Program to print all Servlet Initialization Parameters
InitializeParameterDemoServlet.java
eption,IOException
InitializeParameterDemoServlet.java
h> </tr>");
InitializeParameterDemoServlet.java
InitializeParameterDemoServlet.java
advapps2C
|-WEB-INF
|-web.xml
|-classes
|-InitializeParameterDemoServlet.class
Demo Program to print total number of employees from the database by Servlet
Initialization Parameters
FirstServlet.java
eption,IOException
FirstServlet.java
",user,pwd);
FirstServlet.java
advapps2CB
|-WEB-INF
|-web.xml
|-lib
|-ojdbc6.jar
|-classes
|-FirstServlet.class
Note:
Servlet initialization parameters are key-value pairs where both key and value are String type.
From the servlet we can access these parameters but we cannot modify. i.e we have only getter
methods but not setter methods. Hence Servlet initialization parameters are considered as
deployment time constants.
ServletConfig (I)
<load-on-startup>:
usually servlet class loading,instanitation and execution of init() method will takes place at the
time of first request. It increases processing time of first request when compared with remaining
requests.
To overcome this problem, we should go for <load-on-startup>
If we configured <load-on-startup> then these steps will be performed at the time of server start
up or at application deployment.
ServletConfig (I)
The main advantage of <load-on-startup> is all requests will be processed with uniform response
time.
The main disadvantage of <load-on-startup> is, creating servlet object at the beginning may effect
performance and causes memory problems.
The servlet whose <load-on-startup> value is less, that servlet will be loaded first.
If 2 servlets having same <load-on-startup> value or if the <load-on-startup> value is negative then
we cannot expect order of loading. It is web server vendor dependent.
Demo Program to demonstrate <load-on-startup>:
FirstServlet.java
IOException
FirstServlet.java
SecondServlet.java
IOException
SecondServlet.java
web.xml:
SecondServlet.java
advapps2D
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
|-SecondServlet.class
<servlet-mapping>:
By using this tag, we can map a servlet with <url-pattern>.
Up to Servlet 2.4 version within <servlet-mapping>, a single servlet can map to exactly one <url-
pattern>
But from Servlet 2.5V onwards we can map servlet with multiple url patterns. i.e we can take
mulitple <url-pattern> tags inside <servlet-mapping>.
SecondServlet.java
It is invalid in Servlet 2.4V but valid in Servlet 2.5V.
According to Servlet specification there are 4 types of url-patterns are possible.
- Exact match url-pattern
eg: /test
- Longest Path Prefix url-pattern or directory match url-pattern
eg: /test/test/*
- url-pattern by extension
eg: *.do
- Default url-pattern
eg: /
Q.Which of the following are valid url-patterns ?
SecondServlet.java
*Web container always gives the precedence in the following order
- Exact Match UP
- Longest Path Prefix UP
- UP by extension
- Default UP
Demo Program to demonistrate different types of url-patterns and priority order:
FirstServlet.java
eption,IOException
FirstServlet.java
SecondServlet.java
eption,IOException
SecondServlet.java
ThirdServlet.java
eption, IOException
ThirdServlet.java
DefaultServlet.java
eption,IOException
DefaultServlet.java
web.xml:
DefaultServlet.java
advapps2E
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
|-SecondServlet.class
|-ThirdServlet.class
|-DefaultServlet.class
FS==>/test
SS==>/test/test/*
TS==>*.do
DS===>/
http://localhost:7777/advapps2E/test ==>FS
http://localhost:7777/advapps2E/test/test/durga ==>SS
http://localhost:7777/advapps2E/test/test/durga.do ==>SS
http://localhost:7777/advapps2E/test/durga.do ==>TS
http://localhost:7777/advapps2E/test/anushka ==>DS
Q. How to configure Default Servlet in web.xml and when it will get chance?
We can configure Default Servlet with url-pattern "/".
If no other servlet matched then only default servlet will get chance.
Getting Extra Information from the url
FirstServlet.java
eption,IOException
FirstServlet.java
web.xml
FirstServlet.java
advapps2F
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
FS==>/test/test/*
Eg1:
http://localhost:7777/advapps2F/test/test/durga/software?user=durga&pwd=anushka
Request URI:/advapps2F/test/test/durga/software
Context Path:/advapps2F
Servlet Path:/test/test
Path Info:/durga/software
Query String:user=durga&pwd=anushka
http://localhost:7777/advapps2F/test/test/durga/software
Request URI:/advapps2F/test/test/durga/software
Context Path:/advapps2F
Servlet Path:/test/test
Path Info:/durga/software
Query String:null
http://localhost:7777/advapps2F/test/test
Request URI:/advapps2F/test/test/durga/software
Context Path:/advapps2F
Servlet Path:/test/test
Path Info: null
Query String:null
Configuring welcome files for web application:
It is highly recommended to configure welcome files for our web application. It increases easyness
to use our web application for the end user.
We can configure welcome files in the web.xml as follows...
FirstServlet.java
<welcome-file-list> is the direct child tag of <web-app> and hence we can take anywhere.
We can configure any number of welcome files but the order is important. Web container always
consider from top to bottom.
In any web application index.html acts as default welcome file.If index.html is not available then
index.jsp acts as default welcome file.
If we configured welcome files explicitly then default welcome files concept is not applicable.
welcome files concept is applicable for sub folders also.
Demo Program to demonistrate default welcome files
HydJobsServlet.java
eption,IOException
HydJobsServlet.java
BangaloreJobsServlet.java
eption,IOException
BangaloreJobsServlet.java
PuneJobsServlet.java
eption,IOException
PuneJobsServlet.java
web.xml:
PuneJobsServlet.java
advapps2GW
|-WEB-INF
|-web.xml
|-classes
|-HydJobsServlet.class
|-BangaloreJobsServlet.class
|-PuneJobsServlet.class
http://localhost:7777/advapps2GW
Demo Program to demonistrate customized welcome files
http://localhost:7777/advapps2G ==>home.jsp
http://localhost:7777/advapps2G/durga==>login.jsp
http://localhost:7777/advapps2G/durga/durga1==>home.jsp
http://localhost:7777/advapps2G/durga2==>index.jsp
http://localhost:7777/advapps2G/durga3==>404 Status code
Note:
According to Servlet Specification the value of <welcome-file> tag should be name of the jsp but
not location. Hence it should not starts with "/"
Eg:
<welcome-file>home.jsp</welcome-file>==>valid
<welcome-file>/home.jsp</welcome-file>===>invalid
Configuring Error Pages in web.xml
Configuring error page based on error code:
Configuring Error Pages in web.xml
Note:
Configuring Error Pages in web.xml
<web-app>.
- error page can be either servlet or jsp.
FirstServlet.java
error.jsp:
<h1>Hello your provided input is invalid...plz provide valid input</h1>
error404.jsp:
<h1>Hello Stupid..Plz cross check your url and send valid url</h1>
web.xml:
FirstServlet.java
|-error.jsp
|-error404.jsp
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
If we are sending the request to the FirstServlet then instead of getting ArithmeticException,we
will get error.jsp page response.
If we are sending the request with invalid url-pattern then instead of 404 status code,we will get
error404.jsp page response.
Demo Program 2 for demonistrating error pages
FirstServlet.java
eption,IOException
FirstServlet.java
error401.jsp:
FirstServlet.java
web.xml:
FirstServlet.java
FirstServlet.java
FirstServlet.java
FirstServlet.java
FirstServlet.java
advapps2I
|-login.html
|-error401.jsp
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
Case-1:
We can configure <error-page> either based on exception type or based on error code but not
both simultaneously in the same <error-page> tag.
Eg:
FirstServlet.java
It is invalid
Case-2:
The <exception-type> tag value should be fully qualified name of exception.
FirstServlet.java
It is invalid
Case-3:
The <location> value must be compulsary starts with "/",otherwise application won't be deployed.
FirstServlet.java
It is invalid
<mime-mapping>:
We can use <mime-mapping> to map file extension with the corresponding <mime-type>.
FirstServlet.java
<mime-mapping> is the direct child tag of <web-app> and hence we can take anywhere.
war File
Objective:
- Explain the purpose of war file?
- Describe the contents of war file?
- Explain the process of creation?
war (Web Archieve) provides a convenient way to store resources of web appication in a single
component.
It is a compressed file(zip file) represents total web application which may contains Servlets, JSPs,
XML Files, HTML Pages, JavaScript Files, CSS Files etc..
The main advantage of maintaining web application in the form of war file is Project delivery,
Project transportation and deployment will become easy.
Servlet specification defined a standard structure for the war file and every web server can
provide support for that war file.
Various Commands
jar -cvf mywebapp.war *
Various Commands
jar -xvf mywebapp.war
- Display Table of contents of war File
jar -tvf mywebapp.war
http://localhost:7777/wardemo/login.html
Structure of war File:
advapps2A
| Name of Application | *.html | Static |
|---|
OR *.jpg Context
*.css
Context Root
:::
:::
*.jsp
META-INF
MANIFEST.MF
WEB-INF
web.xml
classes
*.class
lib
*.jar
tags
*.tld
*.tag
Every war file should compulsary contains META-INF folder, which should contain MANIFEST.MF.
i.e META-INF folder and MANIFEST.MF are mandatory for every war file.
META-INF folder contains security related resources like signature files,digital certificates, license
agreements etc which are mandatory for maintaining the web application.
MANIFEST.MF
It represents one web application that contains servlets,jsps,html files...
MANIFEST.MF
It represents an enterprise application which contains servlets,jsps,ejbs,jms components etc..
- Every example is complete and compiles as-is
- Examples are grouped by topic
- Typing programs is the fastest way to learn servlets