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
JCode Cell
1 
2<h1>Now Server Time is :
3<%=new java.util.Date()%>
4</h1>web.xml:
5<web-app>
6<servlet>
7<servlet-name>FirstJSP</servlet-name>
8<jsp-file>/WEB-INF/date.jsp</jsp-file>
9<init-param>
10</init-param>
11</servlet>
12<servlet-mapping>
13<servlet-name>FirstJSP</servlet-name>
14<url-pattern>/test</url-pattern>
15</servlet-mapping>
16</web-app>advapps2B
17

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
JCode Cell
1 
2<web-app>
3 
4<servlet>
5<servlet-name>FirstServlet</servlet-name>
6<servlet-class>FirstServlet</servlet-class>
7<init-param>
8<param-name>user</param-name>
9<param-value>scott</param-value>
10</init-param>
11<init-param>
12<param-name>pwd</param-name>
13<param-value>tiger</param-value>
14</init-param>
15</servlet>
16 
17</web-app>
18

Demo Program

Returns the value associated with specified parameter.

Returns null if the specified paramter is not available.

Example04
JCode Cell
1 
2public String getInitParameter(String pname)
3

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
JCode Cell
1 
2public Enumeration getInitParameterNames()
3

Demo Program to print all Servlet Initialization Parameters

web.xml:

Demo Program to print all Servlet Initialization Parameters

Example07
JCode Cell
1 
2<web-app>
3<servlet>
4<servlet-name>DemoServlet</servlet-name>
5<servlet-class>InitializeParameterDemoServlet</servlet-class>
6<init-param>
7<param-name>PhoneNumber</param-name>
8<param-value>9848012345</param-value>
9</init-param>
10<init-param>
11<param-name>MailId</param-name>
12<param-value>durgaocjp@gmail.com</param-value>
13</init-param>
14<init-param>
15<param-name>UserName</param-name>
16<param-value>scott123</param-value>
17</init-param>
18</servlet>
19<servlet-mapping>
20<servlet-name>DemoServlet</servlet-name>
21<url-pattern>/test</url-pattern>
22</servlet-mapping>
23</web-app>
24

InitializeParameterDemoServlet.java

eption,IOException

Example08
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5import java.util.*;
6public class InitializeParameterDemoServlet extends HttpServlet
7{
8public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
9

InitializeParameterDemoServlet.java

h> </tr>");

Example09
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<center><h1>Intialization Parameters</h1></center><hr>");
5Enumeration e = getInitParameterNames();
6out.println("<table border=2><tr><th>Parameter Name</th><th>Parameter Value</t
7

InitializeParameterDemoServlet.java

Example10
JCode Cell
1 
2while (e.hasMoreElements())
3{
4String pname = (String)e.nextElement();
5String pvalue = getInitParameter(pname);
6

InitializeParameterDemoServlet.java

advapps2C

|-WEB-INF

|-web.xml

|-classes

|-InitializeParameterDemoServlet.class

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

Example11
JCode Cell
1 
2out.println("<tr><td>"+pname+"</td><td>"+pvalue+"</td></tr>");
3}
4out.println("</table>");
5out.println("</body></html>");
6}
7}
8

Initialization Parameters

web.xml:

Initialization Parameters

Example13
JCode Cell
1 
2<web-app>
3<servlet>
4<servlet-name>FirstServlet</servlet-name>
5<servlet-class>FirstServlet</servlet-class>
6<init-param>
7<param-name>user</param-name>
8<param-value>scott</param-value>
9</init-param>
10<init-param>
11<param-name>pwd</param-name>
12<param-value>tiger</param-value>
13</init-param>
14</servlet>
15<servlet-mapping>
16<servlet-name>FirstServlet</servlet-name>
17<url-pattern>/test</url-pattern>
18</servlet-mapping>
19</web-app>
20

FirstServlet.java

eption,IOException

Example14
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5import java.sql.*;
6public class FirstServlet extends HttpServlet
7{
8public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
9

FirstServlet.java

",user,pwd);

Example15
JCode Cell
1 
2{
3String user=getInitParameter("user");
4String pwd=getInitParameter("pwd");
5PrintWriter out = resp.getWriter();
6try{
7Class.forName("oracle.jdbc.OracleDriver");
8Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE
9

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
JCode Cell
1 
2Statement st =con.createStatement();
3ResultSet rs =st.executeQuery("select count(*) from employees");
4while(rs.next())
5{
6int count=rs.getInt(1);
7out.println("<h1>The number of employees:"+count+"</h1>");
8}
9}
10catch(Exception e)
11{
12e.printStackTrace();
13}
14}
15}
16

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
JCode Cell
1 
2public String getServletName()
3public String getInitParameter()
4public Enumeration getInitParamterNames()
5public ServletContext getServletContext()
6

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
JCode Cell
1 
2<web-app>
3<servlet>
4....
5<load-on-startup>10</load-on-startup>
6</servlet>
7..
8</web-app>
9

FirstServlet.java

IOException

Example20
JCode Cell
1 
2import javax.servlet.*;
3import java.io.*;
4public class FirstSevlet extends GenericServlet
5{
6static
7{
8System.out.println("FirstServlet Loading..");
9}
10public FirstSevlet()
11{
12System.out.println("FirstServlet Instantiation..");
13}
14public void init(ServletConfig config) throws ServletException
15{
16System.out.println("FirstServlet init method execution..");
17}
18public void service(ServletRequest req, ServletResponse resp) throws ServletException,
19
Output

FirstServlet Loading..
FirstServlet Instantiation..
FirstServlet init method execution..
      

FirstServlet.java

Example21
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>FirstServlet:Writing servlet by extending GS is very easy</h1>");
5System.out.println("service method called..");
6}
7}
8
Output

<h1>FirstServlet:Writing servlet by extending GS is very easy</h1>
service method called..
      

SecondServlet.java

IOException

Example22
JCode Cell
1 
2import javax.servlet.*;
3import java.io.*;
4public class SecondServlet extends GenericServlet
5{
6static
7{
8System.out.println("SecondServlet Loading..");
9}
10public SecondServlet()
11{
12System.out.println("SecondServlet Instantiation..");
13}
14public void init(ServletConfig config) throws ServletException
15{
16System.out.println("SecondServlet init method execution..");
17}
18public void service(ServletRequest req, ServletResponse resp) throws ServletException,
19
Output

SecondServlet Loading..
SecondServlet Instantiation..
SecondServlet init method execution..
      

SecondServlet.java

web.xml:

Example23
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>SecondServlet:Writing servlet by extending GS is very easy</h1>");
5System.out.println("service method called..");
6}
7}
8
Output

<h1>SecondServlet:Writing servlet by extending GS is very easy</h1>
service method called..
      

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
JCode Cell
1 
2<web-app>
3<servlet>
4<servlet-name>FirstSevlet</servlet-name>
5<servlet-class>FirstSevlet</servlet-class>
6<load-on-startup>20</load-on-startup>
7</servlet>
8<servlet>
9<servlet-name>SecondServlet</servlet-name>
10<servlet-class>SecondServlet</servlet-class>
11<load-on-startup>10</load-on-startup>
12</servlet>
13<servlet-mapping>
14<servlet-name>FirstSevlet</servlet-name>
15<url-pattern>/test</url-pattern>
16</servlet-mapping>
17</web-app>
18

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
JCode Cell
1 
2<servlet-mapping>
3<servlet-name>DemoServlet</servlet-name>
4<url-pattern>/test</url-pattern>
5<url-pattern>/hello</url-pattern>
6<url-pattern>/demo</url-pattern>
7</servlet-mapping>
8

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
JCode Cell
1 
2/test
3/test/*/test
4/test/test/*
5*.task
6/
7/test/test/*.do
8

FirstServlet.java

eption,IOException

Example27
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class FirstServlet extends HttpServlet
6{
7public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
8

FirstServlet.java

Example28
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Hi ...This is First Servlet</h1>");
5}
6}
7
Output

<h1>Hi ...This is First Servlet</h1>
      

SecondServlet.java

eption,IOException

Example29
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class SecondServlet extends HttpServlet
6{
7public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
8

SecondServlet.java

Example30
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Hi ...This is Second Servlet</h1>");
5}
6}
7
Output

<h1>Hi ...This is Second Servlet</h1>
      

ThirdServlet.java

eption, IOException

Example31
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class ThirdServlet extends HttpServlet
6{
7public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
8

ThirdServlet.java

Example32
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Hi ...This is Third Servlet</h1>");
5}
6}
7
Output

<h1>Hi ...This is Third Servlet</h1>
      

DefaultServlet.java

eption,IOException

Example33
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class DefaultServlet extends HttpServlet
6{
7public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
8

DefaultServlet.java

web.xml:

Example34
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Hi ...This is Default Servlet</h1>");
5}
6}
7
Output

<h1>Hi ...This is Default Servlet</h1>
      

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
JCode Cell
1 
2<web-app>
3<servlet>
4<servlet-name>FirstServlet</servlet-name>
5<servlet-class>FirstServlet</servlet-class>
6</servlet>
7<servlet>
8<servlet-name>SecondServlet</servlet-name>
9<servlet-class>SecondServlet</servlet-class>
10</servlet>
11<servlet>
12<servlet-name>ThirdServlet</servlet-name>
13<servlet-class>ThirdServlet</servlet-class>
14</servlet>
15<servlet>
16<servlet-name>DefaultServlet</servlet-name>
17<servlet-class>DefaultServlet</servlet-class>
18</servlet>
19<servlet-mapping>
20<servlet-name>FirstServlet</servlet-name>
21<url-pattern>/test</url-pattern>
22</servlet-mapping>
23<servlet-mapping>
24<servlet-name>SecondServlet</servlet-name>
25<url-pattern>/test/test/*</url-pattern>
26</servlet-mapping>
27<servlet-mapping>
28<servlet-name>ThirdServlet</servlet-name>
29<url-pattern>*.do</url-pattern>
30</servlet-mapping>
31<servlet-mapping>
32<servlet-name>DefaultServlet</servlet-name>
33<url-pattern>/</url-pattern>
34</servlet-mapping>
35</web-app>
36

Getting Extra Information from the url

ServletRequest interface defines the following methods for this

Getting Extra Information from the url

Example37
JCode Cell
1 
2getRequestURI()
3getContextPath()
4getServletPath()
5getPathInfo()
6getQueryString()
7

FirstServlet.java

web.xml

Example38
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Request URI:" +req.getRequestURI()+"</h1>");
5out.println("<h1>Context Path:" +req.getContextPath()+"</h1>");
6out.println("<h1>Servlet Path:" +req.getServletPath()+"</h1>");
7out.println("<h1>Path Info:" +req.getPathInfo()+"</h1>");
8out.println("<h1>Query String:" +req.getQueryString()+"</h1>");
9}
10}
11

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
JCode Cell
1 
2<web-app>
3<servlet>
4<servlet-name>FirstServlet</servlet-name>
5<servlet-class>FirstServlet</servlet-class>
6</servlet>
7<servlet-mapping>
8<servlet-name>FirstServlet</servlet-name>
9<url-pattern>/test/test/*</url-pattern>
10</servlet-mapping>
11</web-app>
12

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
JCode Cell
1 
2<web-app>
3<welcome-file-list>
4<welcome-file>home.jsp</welcome-file>
5<welcome-file>login.jsp</welcome-file>
6<welcome-file>index.jsp</welcome-file>
7</welcome-file-list>
8</web-app>
9

Demo Program to demonstrate default welcome files

index.jsp:

Demo Program to demonstrate default welcome files

Example42
JCode Cell
1 
2<h1>Welcome to Durgajobs information<br/><hr/>
3<a href="/advapps2GW/test1">Hyderabad Jobs Info</a><br/>
4<a href="/advapps2GW/test2">Bangalore Jobs Info</a><br/>
5<a href="/advapps2GW/test3">Pune Jobs Info</a></h1>
6

HydJobsServlet.java

eption,IOException

Example43
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class HydJobsServlet extends HttpServlet
6{
7public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
8

HydJobsServlet.java

Example44
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Hyderabad Jobs Info</h1>");
5}
6}
7
Output

<h1>Hyderabad Jobs Info</h1>
      

BangaloreJobsServlet.java

eption,IOException

Example45
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class BangaloreJobsServlet extends HttpServlet
6{
7public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
8

BangaloreJobsServlet.java

Example46
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Bangalore Jobs Info</h1>");
5}
6}
7
Output

<h1>Bangalore Jobs Info</h1>
      

PuneJobsServlet.java

eption,IOException

Example47
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5public class PuneJobsServlet extends HttpServlet
6{
7public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
8

PuneJobsServlet.java

web.xml:

Example48
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4out.println("<h1>Pune Jobs Info</h1>");
5}
6}
7
Output

<h1>Pune Jobs Info</h1>
      

PuneJobsServlet.java

advapps2GW

|-WEB-INF

|-web.xml

|-classes

|-HydJobsServlet.class

|-BangaloreJobsServlet.class

|-PuneJobsServlet.class

http://localhost:7777/advapps2GW

Example49
JCode Cell
1 
2<web-app>
3<servlet>
4<servlet-name>FirstServlet</servlet-name>
5<servlet-class>HydJobsServlet</servlet-class>
6</servlet>
7<servlet>
8<servlet-name>SecondServlet</servlet-name>
9<servlet-class>BangaloreJobsServlet</servlet-class>
10</servlet>
11<servlet>
12<servlet-name>ThirdServlet</servlet-name>
13<servlet-class>PuneJobsServlet</servlet-class>
14</servlet>
15<servlet-mapping>
16<servlet-name>FirstServlet</servlet-name>
17<url-pattern>/test1</url-pattern>
18</servlet-mapping>
19<servlet-mapping>
20<servlet-name>SecondServlet</servlet-name>
21<url-pattern>/test2</url-pattern>
22</servlet-mapping>
23<servlet-mapping>
24<servlet-name>ThirdServlet</servlet-name>
25<url-pattern>/test3</url-pattern>
26</servlet-mapping>
27</web-app>
28

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
JCode Cell
1 
2<web-app>
3<welcome-file-list>
4<welcome-file>home.jsp</welcome-file>
5<welcome-file>login.jsp</welcome-file>
6<welcome-file>index.jsp</welcome-file>
7</welcome-file-list>
8</web-app>
9

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
JCode Cell
1 
2<web-app>
3<error-page>
4<exception-type>java.lang.ArithmeticException</exception-type>
5<location>/error.jsp</location>
6</error-page>
7</web-app>
8

Configuring Error Pages in web.xml

Note:

Example54
JCode Cell
1 
2<web-app>
3<error-page>
4<error-code>404</error-code>
5<location>/test1</location>
6</error-page>
7</web-app>
8

Configuring Error Pages in web.xml

<web-app>.

  • error page can be either servlet or jsp.
Example55
JCode Cell
1 
2<error-page> is the direct child tag of <web-app> and hence we can take anywhere within
3

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
JCode Cell
1 
2public class FirstSevlet extends HttpServlet
3{
4public void doGet(..)...
5{
6PrintWriter out = resp.getWriter();
7out.println(10/0);
8}
9}
10

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
JCode Cell
1 
2<web-app>
3 
4<servlet>
5<servlet-name>FirstSevlet</servlet-name>
6<servlet-class>FirstSevlet</servlet-class>
7</servlet>
8 
9<servlet-mapping>
10<servlet-name>FirstSevlet</servlet-name>
11<url-pattern>/test</url-pattern>
12</servlet-mapping>
13 
14<error-page>
15<exception-type>java.lang.ArithmeticException</exception-type>
16<location>/error.jsp</location>
17</error-page>
18 
19<error-page>
20<error-code>404</error-code>
21<location>/error404.jsp</location>
22</error-page>
23</web-app>advapps2H
24

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
JCode Cell
1 
2<h1>
3<form action ="/advapps2I/test" >
4Enter Name: <input type="text" name="uname"><br>
5<input type="submit" value="Login"/>
6</form></h1>
7

FirstServlet.java

eption,IOException

Example61
JCode Cell
1 
2import javax.servlet.*;
3import javax.servlet.http.*;
4import java.io.*;
5import java.util.*;
6public class FirstServlet extends HttpServlet
7{
8public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletExc
9

FirstServlet.java

error401.jsp:

Example62
JCode Cell
1 
2{
3PrintWriter out = resp.getWriter();
4String name= req.getParameter("uname");
5if(name.equals("durga"))
6{
7out.println("<h1>your authentication is correct...you can avail the facilities</h1>");
8}
9else
10{
11resp.sendError(401);
12}
13}
14}
15

FirstServlet.java

web.xml:

Example63
JCode Cell
1 
2<h1>Your authentication is failed....plz provide valid credentials...<br/>
3<a href="/advapps2I/login.html">Click here to login once again</a></h1><br>
4

FirstServlet.java

Example64
JCode Cell
1 
2<web-app>
3

FirstServlet.java

Example65
JCode Cell
1 
2<error-page>
3<error-code>401</error-code>
4<location>/error401.jsp</location>
5</error-page>
6

FirstServlet.java

Example66
JCode Cell
1 
2<servlet>
3<servlet-name>FirstServlet</servlet-name>
4<servlet-class>FirstServlet</servlet-class>
5</servlet>
6

FirstServlet.java

Example67
JCode Cell
1 
2<servlet-mapping>
3<servlet-name>FirstServlet</servlet-name>
4<url-pattern>/test</url-pattern>
5</servlet-mapping>
6

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
JCode Cell
1 
2</web-app>
3

FirstServlet.java

It is invalid

Case-2:

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

Example69
JCode Cell
1 
2<error-page>
3<exception-type>java.lang.AE</exception-type>
4<error-code>404</error-code>
5<location>/error.jsp</location>
6</error-page>
7

FirstServlet.java

It is invalid

Case-3:

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

Example70
JCode Cell
1 
2<error-page>
3<exception-type>IOException</exception-type>
4<location>/error.jsp</location>
5</error-page>
6

FirstServlet.java

It is invalid

<mime-mapping>:

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

Example71
JCode Cell
1 
2<error-page>
3<error-code>404</error-code>
4<location>error.jsp</location>
5</error-page>
6

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
JCode Cell
1 
2<mime-mapping>
3<extension>durga</extension>
4<mime-type>application/pdf</mime-type>
5</mime-mapping>
6

Various Commands

jar -cvf mywebapp.war *

Example73
JCode Cell
1 
2Creation of war File (zip file):
3

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
JCode Cell
1 
2Extraction of war File (unzip operation)
3

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
JCode Cell
1 
2war(web archieve)
3

MANIFEST.MF

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

Example77
JCode Cell
1 
2ear(enterprise archieve):
3
📝 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