The url-pattern element of a servlet-mapping or a filter-mapping associates a filter or servlet with a set of URLs. When a request arrives, the container uses a simple procedure for matching the URL in the request with a url-pattern in the web.xml file. Section 4.7.2 describes the servlet-mapping element. Section 4.8.2 describes the filter-mapping element.
Note that HydraExpress matches a URL pattern as a sequence of bytes rather than a sequence of characters. HydraExpress considers URL-escaped and unescaped sequences to be identical. In other words, the URL pattern * is identical to the URL pattern *. To represent a character pattern that may have more than one byte sequence, add a mapping for each byte sequence.
In contrast, no two servlet-mapping elements in the same application may use the same url-pattern. If the web.xml file contains two identical mappings to different servlets, the container makes no guarantees about which servlet the container calls for a given request. However, two servlets may use overlapping url-pattern elements. In that case, the matching procedure determines which servlet the container calls.
A request may match more than one servlet-mapping in a given context. The servlet container uses a straightforward matching procedure to determine the best match. The matching procedure has four simple rules. First, the container prefers an exact path match over a wildcard path match. Second, the container prefers to match the longest pattern. Third, the container prefers path matches over filetype matches. Finally, the pattern / always matches any request that no other pattern matches (see Section 4.3.3).
Figure 4 illustrates the matching process for a context. Since the container prefers to match the longest pattern, a URL that includes /Catalog/search/ always matches the mapping for catalogSearch rather than the mapping for catalogBrowse.
The default mapping is often directed to the first page of an application. Explicitly providing a default mapping also ensures that malformed URL requests into the application return are handled by the application rather than returning an error.
I have manually configured web.xml for my application. Now, I'm facing issues while running my application. I'm trying to access my servlet from my jsp page. But, it is throwing error as page not found.
If you change url-pattern of AddPhotoServlet from /AddPhotoServlet to /MyUrl. Then, AddPhotoServlet servlet can be accessible by using /MyUrl. Good for the security reason, where you want to hide your actual page URL.
Servlet-mapping has two child tags, url-pattern and servlet-name. url-pattern specifies the type of urls for which, the servlet given in servlet-name should be called. Be aware that, the container will use case-sensitive for string comparisons for servlet matching.
Third specification of url-mapping,A mapping that contains the pattern / matches a request if no other pattern matches. This is the default mapping. The servlet mapped to this pattern is called the default servlet.
The /* on a servlet overrides all other servlets, including all servlets provided by the servletcontainer such as the default servlet and the JSP servlet. Whatever request you fire, it will end up in that servlet. This is thus a bad URL pattern for servlets. Usually, you'd like to use /* on a Filter only. It is able to let the request continue to any of the servlets listening on a more specific URL pattern by calling FilterChain#doFilter().
The / doesn't override any other servlet. It only replaces the servletcontainer's built in default servlet for all requests which doesn't match any other registered servlet. This is normally only invoked on static resources (CSS/JS/image/etc) and directory listings. The servletcontainer's built in default servlet is also capable of dealing with HTTP cache requests, media (audio/video) streaming and file download resumes. Usually, you don't want to override the default servlet as you would otherwise have to take care of all its tasks, which is not exactly trivial (JSF utility library OmniFaces has an open source example). This is thus also a bad URL pattern for servlets. As to why JSP pages doesn't hit this servlet, it's because the servletcontainer's built in JSP servlet will be invoked, which is already by default mapped on the more specific URL pattern *.jsp.
Then there's also the empty string URL pattern . This will be invoked when the context root is requested. This is different from the approach that it isn't invoked when any subfolder is requested. This is most likely the URL pattern you're actually looking for in case you want a "home page servlet". I only have to admit that I'd intuitively expect the empty string URL pattern and the slash URL pattern / be defined exactly the other way round, so I can understand that a lot of starters got confused on this. But it is what it is.
In case you actually intend to have a front controller servlet, then you'd best map it on a more specific URL pattern like *.html, *.do, /pages/*, /app/*, etc. You can hide away the front controller URL pattern and cover static resources on a common URL pattern like /resources/*, /static/*, etc with help of a servlet filter. See also How to prevent static resources from being handled by front controller servlet which is mapped on /*. Noted should be that Spring MVC has a built in static resource servlet, so that's why you could map its front controller on / if you configure a common URL pattern for static resources in Spring. See also How to handle static content in Spring MVC?
Perhaps you need to know how urls are mapped too, since I suffered 404 for hours. There are two kinds of handlers handling requests. BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping. When we defined a servlet-mapping, we are using SimpleUrlHandlerMapping. One thing we need to know is these two handlers share a common property called alwaysUseFullPath which defaults to false.
The essential difference between /* and / is that a servlet with mapping /* will be selected before any servlet with an extension mapping (like *.html), while a servlet with mapping / will be selected only after extension mappings are considered (and will be used for any request which doesn't match anything else---it is the "default servlet").
Either will be selected only after servlet mappings which are exact matches (like /foo/bar) and those which are path mappings longer than /* (like /foo/*). Note that the empty string mapping is an exact match for the context root ( :port/context/).
As noted by others, there is no way to filter with regular expression matching using only the basic servlet filter features. The servlet spec is likely written the way it is to allow for efficient matching of urls, and because servlets predate the availability of regular expressions in java (regular expressions arrived in java 1.4).
Java web applications use a deployment descriptor file to determine how URLs mapto servlets, which URLs require authentication, and other information. This fileis named web.xml, and resides in the app's WAR under the WEB-INF/ directory.web.xml is part of the servlet standard for web applications.
web.xml defines mappings between URL paths and the servlets that handlerequests with those paths. The web server uses this configuration to identifythe servlet to handle a given request and call the class method that correspondsto the request method (e.g., the doGet() method for HTTP GET requests).
The element declares the servlet, including a name used to refer tothe servlet by other elements in the file, the class to use for the servlet, andinitialization parameters. You can declare multiple servlets using the sameclass with different initialization parameters. The name for each servlet mustbe unique across the deployment descriptor.
The element specifies a URL pattern and the name of adeclared servlet to use for requests whose URL matches the pattern. The URLpattern can use an asterisk (*) at the beginning or end of the pattern toindicate zero or more of any character. The standard does not support wildcardsin the middle of a string, and does not allow multiple wildcards in onepattern. The pattern matches the full path of the URL, starting with andincluding the forward slash (/) following the domain name.
With this example, a request for the URL is handled by the TeamServlet class,with the teamColor parameter equal to blue and the bgColor parameter equalto #0000CC. The servlet can get the portion of the URL path matched by thewildcard using the ServletRequest object's getPathInfo() method.
The servlet can access its initialization parameters by getting its servletconfiguration using its own getServletConfig() method, then calling thegetInitParameter() method on the configuration object using the name of theparameter as an argument.
App Engine supports automatic compilation and URL mapping for JSPs. AJSP file in the application's WAR (outside of WEB-INF/) whose filename ends in.jsp is compiled into a servlet class automatically, and mapped to the URLpath equivalent to the path to the JSP file from the WAR root. For example, ifan app has a JSP file named start.jsp in a subdirectory named register/ inits WAR, App Engine compiles it and maps it to the URL path/register/start.jsp.
If you want more control over how the JSP is mapped to a URL, you can specifythe mapping explicitly by declaring it with a element in thedeployment descriptor. Instead of a element, you specify a element with the path to the JSP file from the WAR root. The element for the JSP can contain initialization parameters.
When the URLs for your site represent paths to static files or JSPs in your WAR,it is often a good idea for paths to directories to do something useful as well.A user visiting the URL path /help/accounts/password.jsp for information onaccount passwords may try to visit /help/accounts/ to find a page introducingthe account system documentation. The deployment descriptor can specify a listof filenames that the server should try when the user accesses a path thatrepresents a WAR subdirectory (that is not already explicitly mapped to aservlet). The servlet standard calls this the "welcome file list." 2b1af7f3a8