Nearby lessons
11 of 34Servlet - Web Application Directory Structure
- Understand UNIT-2: Directory Structure and Deployment of Web Application
- See complete working code examples
Web Application Directory Structure is an essential part of the Java Servlet technology. This lesson explains UNIT-2: Directory Structure and Deployment of Web Application and Configuring JSP in web.xml with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.
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, because 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 compulsory it should be concrete class
but not abstract class or interface.
Configuring JSP in web.xml
Usually we can place JSPs within the context root directly. In this case end user can access directly
by its name.
But if the JSP is secured then it is not recommended to place in the context root directly. We
have to place such type of jsps inside WEB-INF and we have to provide access through url-pattern.
We can configure JSP by using <jsp-file> tag in web.xml
- Key ideas of Servlet - Web Application Directory Structure explained simply
- Ready-to-use code examples
- Exam-style questions at the end