Explain the elements of web.xml

Answered

Explain the elements of web.xml

Ninja Asked on 19th September 2018 in Servlet.
Add Comment
1 Answer(s)
Best answer

The web.xml is the deployment descriptor for WAR file which is located in the WEB-INF directory of the web application. It is used to configure Servlet and other server side resources used in the web application. Each servlet must appear in a web.xml file , to add a servlet to an existing context, add a servlet and servlet-mapping element to the context.

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE web-app

     PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN”

     “http://java.sun.com/j2ee/dtds/web-app_2_4.dtd“>

 <web-app>

     <servlet>

         <servlet-name>HelloServlet</servlet-name>

         <servlet-class>com.demo.HelloServlet</servlet-class>

     </servlet>

      <servlet-mapping>

         <servlet-name>HelloServlet</servlet-name>

         <url-pattern>/HelloServlet</url-pattern>

     </servlet-mapping>

</web-app>

The first entry, under the root servlet element in web.xml, defines a name for the servlet and specifies the compiled class that executes the servlet. The servlet element also contains definitions for initialization attributes and security roles for the servlet.

The second entry in web.xml, under the servlet-mapping element, defines the URL pattern that calls this servlet.

Ninja Answered on 19th September 2018.
Add Comment