Nearby lessons

12 of 34

Servlet - web.xml (Deployment Descriptor)

📌 What You Will Learn
  • Understand Demo Program
  • Understand Demo Program to print all Servlet Initialization Parameters
  • Understand Initialization Parameters
  • Understand ServletConfig
  • See complete working code examples

web.xml (Deployment Descriptor) is an essential part of the Java Servlet technology. This lesson explains Demo Program, Demo Program to print all Servlet Initialization Parameters and InitializeParameterDemoServlet.java with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

Demo Program

date.jsp:

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

Example02
Loading editor…

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.

Example03
Loading editor…

Demo Program

Returns the value associated with specified parameter.

Returns null if the specified paramter is not available.

Example04
Loading editor…

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.

Example05
Loading editor…

Demo Program to print all Servlet Initialization Parameters

web.xml:

Demo Program to print all Servlet Initialization Parameters

Example07
Loading editor…

InitializeParameterDemoServlet.java

eption,IOException

Example08
Loading editor…

InitializeParameterDemoServlet.java

h> </tr>");

Example09
Loading editor…

InitializeParameterDemoServlet.java

Example10
Loading editor…

InitializeParameterDemoServlet.java

advapps2C

|-WEB-INF

|-web.xml

|-classes

|-InitializeParameterDemoServlet.class

Demo Program to print total number of employees from the database by Servlet

Example11
Loading editor…

Initialization Parameters

web.xml:

Initialization Parameters

Example13
Loading editor…

FirstServlet.java

eption,IOException

Example14
Loading editor…

FirstServlet.java

",user,pwd);

Example15
Loading editor…

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.

Example16
Loading editor…

ServletConfig (I)

For every servlet web container creates one ServletConfig object to hold its configuration

information.

By using ServletConfig object, servlet can get its configuration information.

ServletConfig defines the following 4 methods

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.

Example18
Loading editor…

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

Example19
Loading editor…

FirstServlet.java

IOException

Example20
Loading editor…

FirstServlet.java

Example21
Loading editor…

SecondServlet.java

IOException

Example22
Loading editor…

SecondServlet.java

web.xml:

Example23
Loading editor…

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

Example24
Loading editor…

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 ?

Example25
Loading editor…

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 demonstrate different types of url-patterns and priority order:

Example26
Loading editor…

FirstServlet.java

eption,IOException

Example27
Loading editor…

FirstServlet.java

Example28
Loading editor…

SecondServlet.java

eption,IOException

Example29
Loading editor…

SecondServlet.java

Example30
Loading editor…

ThirdServlet.java

eption, IOException

Example31
Loading editor…

ThirdServlet.java

Example32
Loading editor…

DefaultServlet.java

eption,IOException

Example33
Loading editor…

DefaultServlet.java

web.xml:

Example34
Loading editor…

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.

Example35
Loading editor…

Getting Extra Information from the url

ServletRequest interface defines the following methods for this

Getting Extra Information from the url

Example37
Loading editor…

FirstServlet.java

web.xml

Example38
Loading editor…

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 ease

to use our web application for the end user.

We can configure welcome files in the web.xml as follows...

Example39
Loading editor…

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.

Example40
Loading editor…

Demo Program to demonstrate default welcome files

index.jsp:

Demo Program to demonstrate default welcome files

Example42
Loading editor…

HydJobsServlet.java

eption,IOException

Example43
Loading editor…

HydJobsServlet.java

Example44
Loading editor…

BangaloreJobsServlet.java

eption,IOException

Example45
Loading editor…

BangaloreJobsServlet.java

Example46
Loading editor…

PuneJobsServlet.java

eption,IOException

Example47
Loading editor…

PuneJobsServlet.java

web.xml:

Example48
Loading editor…

PuneJobsServlet.java

advapps2GW

|-WEB-INF

|-web.xml

|-classes

|-HydJobsServlet.class

|-BangaloreJobsServlet.class

|-PuneJobsServlet.class

http://localhost:7777/advapps2GW

Example49
Loading editor…

Demo Program to demonstrate customized welcome files

advapps2G

|-home.jsp

|-durga

|-login.jsp

|-durga1

|-home.jsp

|-login.jsp

|-durga2

|-index.jsp

|-durga3

|-durga2.jsp

|-form.html

|-WEB-INF

|-web.xml

web.xml:

Demo Program to demonstrate 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

Example51
Loading editor…

Configuring Error Pages in web.xml

It is not recommended to send error information directly to the end user. We have to convert that

java specific error information into end user understandable form.

We can achieve this by configuring error pages in web.xml.

We can configure error page either based on exception type or based on error code.

Configuring error page based on exception type:

Configuring Error Pages in web.xml

Configuring error page based on error code:

Example53
Loading editor…

Configuring Error Pages in web.xml

Note:

Example54
Loading editor…

Configuring Error Pages in web.xml

<web-app>.

  • error page can be either servlet or jsp.
Example55
Loading editor…

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:

Example56
Loading editor…

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.

Example57
Loading editor…

Sending Error Code Programatically

We can set error code programatically. HttpServletResponse interface defines the following

method for this.

public void sendError(int errorCode)

Eg: resp.sendError(401);

Demo Program 2 for demonistrating error pages

login.html:

Demo Program 2 for demonistrating error pages

Example60
Loading editor…

FirstServlet.java

eption,IOException

Example61
Loading editor…

FirstServlet.java

error401.jsp:

Example62
Loading editor…

FirstServlet.java

web.xml:

Example63
Loading editor…

FirstServlet.java

Example64
Loading editor…

FirstServlet.java

Example65
Loading editor…

FirstServlet.java

Example66
Loading editor…

FirstServlet.java

Example67
Loading editor…

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:

Example68
Loading editor…

FirstServlet.java

It is invalid

Case-2:

The <exception-type> tag value should be fully qualified name of exception.

Example69
Loading editor…

FirstServlet.java

It is invalid

Case-3:

The <location> value must be compulsory starts with "/",otherwise application won't be deployed.

Example70
Loading editor…

FirstServlet.java

It is invalid

<mime-mapping>:

We can use <mime-mapping> to map file extension with the corresponding <mime-type>.

Example71
Loading editor…

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.

Example72
Loading editor…

Various Commands

jar -cvf mywebapp.war *

Example73
Loading editor…

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

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

Example74
Loading editor…

MANIFEST.MF

If there is any jar file which is common to several web applications,then it is not recommended to

place that jar file at application level.We have to place that jar file at some common location

outside of web application and we can define that path in MANIFEST.MF.

ie MANIFEST.MF contains the classpath of common jar files stored outside of web application.i.e

The library dependencies of web application,we can define in this file only.

The resources present inside META-INF and WEB-INF folders cannot be accessed directly. if we are

trying to access then we will get 404 status code.

http://localhost:7777/wardemo/META-INF/MANIFEST.MF

http://localhost:7777/wardemo/WEB-INF/web.xml

jar vs war vs ear:

1.jar(Java archieve):

It contains a group of .class files

MANIFEST.MF

It represents one web application that contains servlets,jsps,html files...

Example76
Loading editor…

MANIFEST.MF

It represents an enterprise application which contains servlets,jsps,ejbs,jms components etc..

Example77
Loading editor…
📝 Key Takeaways
  • Key ideas of Servlet - web.xml (Deployment Descriptor) explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.