73

What is the use of web.xml and why do we use?

<filter>
        <filter-name>wicket.mysticpaste</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>com.mysticcoders.WicketApplication</param-value>
        </init-param>
    </filter>

 <filter-mapping>
  <filter-name>wicket.mysticpaste</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

What does this filer and filermapping do?

3
  • see stackoverflow.com/questions/2311065/…
    – JoseK
    Dec 27, 2010 at 13:11
  • note that web.xml is all lower-case
    – Bozho
    Dec 27, 2010 at 14:19
  • 15
    He didn't ask why he's using this; he asked why we, as a community, use web.xml files. For his specific web.xml, he asked what it is doing. There's nothing wrong with this question.
    – ineedahero
    Nov 29, 2016 at 15:56

6 Answers 6

86

Generally speaking, this is the configuration file of web applications in java. It instructs the servlet container (tomcat for ex.) which classes to load, what parameters to set in the context, and how to intercept requests coming from browsers.

There you specify:

  • what servlets (and filters) you want to use and what URLs you want to map them to
  • listeners - classes that are notified when some events happen (context starts, session created, etc)
  • configuration parameters (context-params)
  • error pages, welcome files
  • security constraints

In servlet 3.0 many of the web.xml parts are optional. These configurations can be done via annotations (@WebServlet, @WebListener)

13

The web.xml file is the deployment descriptor for a Servlet-based Java web application (which most Java web apps are). Among other things, it declares which Servlets exist and which URLs they handle.

The part you cite defines a Servlet Filter. Servlet filters can do all kinds of preprocessing on requests. Your specific example is a filter had the Wicket framework uses as its entry point for all requests because filters are in some way more powerful than Servlets.

1

It says all the requests to go through WicketFilter


Also, if you use wicket WicketApplication for application level settings. Like URL patterns and things that are true at app level


This is what you need really, http://wicket.apache.org/learn/examples/helloworld.html

1

Servlet to be accessible from a browser, then must tell the servlet container what servlets to deploy, and what URL's to map the servlets to. This is done in the web.xml file of your Java web application.

use web.xml in servlet

<servlet>
    <description></description>
    <display-name>servlet class name</display-name>
    <servlet-name>servlet class name</servlet-name>
    <servlet-class>servlet package name/servlet class name</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servlet class name</servlet-name>
    <url-pattern>/servlet class name</url-pattern>
</servlet-mapping>

manly use web.xml for servlet mapping.

1
  • This does not really answer the why and what in the question.
    – Eric J.
    Feb 6, 2013 at 18:44
0

It's the default configuration for a Java web application; it's required.

WicketFilter

is applied to every HTTP request that's sent to this web app.

2
  • I stumbled randomly upon this post. Correction: It WAS required. It is no longer required. Feb 3, 2019 at 0:15
  • 1
    Nine years later.
    – duffymo
    Feb 3, 2019 at 1:38
0

Web.xml is called as deployment descriptor file and its is is an XML file that contains information on the configuration of the web application, including the configuration of servlets.

Not the answer you're looking for? Browse other questions tagged or ask your own question.