- Java EE Tutorials
- Servlets Tutorials
- Servlets Interview Questions
To understand web.xml, first let us uderstand what is xml?
- xml stands Extensible Markup Language.
- xml is markup language much like html.
- Xml defines set of rules for encoding document in a format that readable by both as human and machine.
- Xml was designed to carry and store data, not to display data. Xml is designed to be self-descriptive.
Web.xml defines mapping between URL paths and servlets that handle requests with those paths. The web.xml file provides configuration and deployment deployment information for the Web components that comprise a Web application. The web.xml descriptor files represents the core of the java application. The web.xml file is located in WEB-INF directory of web application.
Example of web.xml File:
<web-app> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>servletexample.createHelloWorlExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloworldExample/*</url-pattern> </servlet-mapping> </web-app>
Elements in Web.xml
Description of elements Used in web.xml file:
- <web-aap>: This element represents whole application of web.xml file
- <servlet>: This is the sub element of and represents the servlet
- <servlet-name>: This is the sub element of servlet and used to represents the name of servlet
- <servlet-class>: This is the sub element of servlet and used to represents the class of servlet
- <servlet-mapping>: This is the sub element of and used to map the servlet
- <url-pattern>: This is the sub element of servlet-mapping and used to client side to invoke the servlet
Web.xml Tags
Tags used in web.xml file are:
- Welcome-file-list tag; This tag is used to specify the default page of web application if none is specified.
Example:<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
In the above example index.jsp used as web page for web application
- Session config tag: This tag is used to specify the session configuration parameter
Example:<session-config> <session-timeout> 15 </session-timeout> </session-config>
In the above example session-config tag contains another tag session-timeout which specifies the http session timeout. The time specify in minutes.
- Error page tag: This tag is used to specify the error occurred in the while weblogic server is responding ti a HTTP request, returns an HTML page that displays either the HTTP error code.
<error-page> <error-code>105</error-code> <location>/jsp/error/PageNotFound.jsp</location> </error page>
In the above example error-page tag specify the error code as 105 and location describes the location of the jsp page.
Advantages of web.xml file
- The first benefit of the xml is we can write it in our won markup language. There is a no restricted to limited sets of tags. By defining our own tag we can create a markup language in terms of specific problem.
- Searching the data is easy and efficient.
- Completely compatible with Java™ and 100% portable.
Previous Tutorial : Servlet Example || Next Tutorial : Servlet Context