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?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
theJava
  • 14,620
  • 45
  • 131
  • 172
  • see http://stackoverflow.com/questions/2311065/what-is-web-xml-file-and-what-all-things-can-i-do-with-it – JoseK Dec 27 '10 at 13:11
  • note that web.xml is all lower-case – Bozho Dec 27 '10 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 '16 at 15:56

6 Answers6

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)

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
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.

Yash
  • 5,459
  • 3
  • 17
  • 29
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
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.

su-
  • 3,116
  • 3
  • 32
  • 42
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

Nishant
  • 54,584
  • 13
  • 112
  • 127
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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
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.

Ravikumar
  • 41
  • 5