web.xml explained

The web.xml file is a necessary component of a Java Web Application. This file is located under the WEB-INF folder. This file is used to map/match the URL to a servlet or a page. Just note that the value in <servlet-name> tag matches in <servlet> tag and <servlet-mapping> tag. <servlet-class> would be the Java class, and <url-pattern> would be the URL.

Here’s an example for URL

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>file</servlet-name>
        <servlet-class>mysite.server.FileServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>file</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Here’s an example for a page, in this case a jsp file.

(/*) means any other URL , In this example it would be any other URL after /login

    <servlet>
        <servlet-name>login</servlet-name>
        <jsp-file>/register/login.jsp</jsp-file>
    </servlet>

    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login/*</url-pattern>
    </servlet-mapping>

we can also initialize servlet parameters in web.xml. Here’s an example below:

    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>mysite.server.Location</servlet-class>
        <init-param>
            <param-name>location</param-name>
            <param-value>cebu</param-value>
        </init-param>
    </servlet>


For more information on web.xml, here’s a good tutorial.

https://developers.google.com/appengine/docs/java/config/webxml

Advertisements

3 thoughts on “web.xml explained

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s