Nearby lessons

11 of 34

Servlet - Web Application Directory Structure

📌 What You Will Learn
  • 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 *.htmlStaticPublicly

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.

ResourseLocation
1) Static Context1) Within the Context Root directly
2) JSP Pages2) Within the Context Root directly. If JSP is

secured then we have to place inside WEB-INF

Example01
JCode Cell
1 
2Directory Structure of web application
3web.xml
4war file
5Directory Structure of web application:
6

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

Example02
JCode Cell
1 
2Servlet Classes 3) Inside Classes Folder
3Deployment Descriptor (web.xml) 4) Inside WEB-INF directly
4Tag Libraries 5) Within WEB-INF either directly OR indirectly
5JAR Files 6) Inside lib Folder
6Java Class Files 7) Inside Classes Folder
7

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:

Example03
JCode Cell
1 
2Inside classes folder(.class)
3Inside lib folder(.jar )
4Anywhere we can place but we have to set classpath explicitly.
5

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...

Example04
JCode Cell
1 
2<web-app> Around 27
3<description> Top Level
4<display-name> Tags
5<servlet>
6<servlet-mapping>
7<welcome-file-list>
8<error-page>
9<mime-mapping>
10<context-param>
11<security-constraint>
12<jsp-config>
13<session-config>
14<filter>
15<filter-mapping>
16<listener>
17...
18</web-app>
19

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.

Example05
JCode Cell
1 
2<servlet> 9 Child Tags
3<description>
4<display-name>
5<icon>
6<servlet-name>
7<servlet-class> or <jsp-file>
8<init-param>
9<load-on-startup>
10<run-as>
11<security-role-ref>
12</servlet>
13

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.

Example06
JCode Cell
1 
2<web-app> Deployer's Name OR Logical
3<servlet> Name
4<servlet-name>DemoServlet</servlet-name>
5<servlet-class>FirstServlet</servlet-class>
6</servlet> Original Name OR Developer's Name
7 
8<servlet-mapping>
9<servlet-name>DemoServlet</servlet-name>
10<url-pattern>/test</url-pattern> End User's Name
11</servlet-mapping>
12</web-app>
13

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.

Example07
JCode Cell
1 
2public class FirstSevlet extends HttpServlet
3{
4public void doGet(..)...
5{
6PrintWriter out = resp.getWriter();
7String name=getServletName();
8 
9out.println("<h1>Logical Name:"+name+"</h1>");
10}
11}
12

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 Takeaways
  • Key ideas of Servlet - Web Application Directory Structure explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3