0

Im deploying a webapplication to tomcat 8 (renaming to ROOT.war) because the url pattern was set to / I thought that all requests would get directed the servlet. But that wasnt the case, eventually i realized that if I was starting the url with a ? such as

http://localhost:8080/?search=fred

it would not work, but without the ? it would work

http://localhost:8080/search=fred

Why is this ?

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"> 
<display-name>Widget</display-name>
<servlet>
<servlet-name>WidgetServlet</servlet-name>
<servlet-class>com.jthink.WidgetServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>WidgetServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • `?` in path segment is not allowed, that's why it didn't work. I updated my answer with few more details, see if that helps – Arkantos Mar 09 '15 at 15:34
  • I think my analysis is incorrect. When try a sample web-app in my local with `localhost:8080/MavenWeb/?src=fld`, my servlet was able to intercept it without any issues – Arkantos Mar 09 '15 at 16:15

2 Answers2

1

If you want your Servlet to serve all URLs then url-pattern should be like this

<url-pattern>/*</url-pattern>

As the name suggests, it should be RegEx pattern. When you say / - it means to look for single occurrence of / in URL. But when you have multiple slashes in URL, something like

http://stackoverflow.com/questions/28945202/

then it should be /* which means you're asking it to look for zero or more occurrences of slash.

Hope that makes it clear :)

Arkantos
  • 6,530
  • 2
  • 16
  • 36
  • Ok still having problems, changed to /* but then every url (such as links css stylesheets and images) to in the page returned was also being handled by the servlet. So I then rethought this so servlet only handles /action that was okay but I wanted it to handle things like /action/id1, /action/id2 and that reuired making the pattern /action/* and now Im getting the stylesheet link problem again – Paul Taylor Mar 09 '15 at 17:02
  • Do you have `action` in static files urls as well ?? – Arkantos Mar 09 '15 at 17:05
  • Can you post a sample URL for static file ? – Arkantos Mar 09 '15 at 17:05
  • They are relative like ../style etc – Paul Taylor Mar 09 '15 at 17:08
  • Then that's the problem.. As you made them relative `action` is appended to your static files... Are these files directly under your root folder ? If so then you can access them with this.. `/js/app.js` – Arkantos Mar 09 '15 at 17:12
  • Actually I think Ive solved it, (the files are directly in root folder of deployed servlet application), because now action/id1 as path style folder is now ../../style not just ../style which resolves to action/style and hence gets intercepted by Servlet again, chnaging pathsa to ../../style and it worked. I could put in absolute paths but doesnt this depends where deployed, can I specify url just relative to the root – Paul Taylor Mar 09 '15 at 17:22
  • Yes we usually keep all our static files outside WEB-INF. Any resource inside WEB-INF is private and accessible only through servlets or filters. If you have a file in `webapp-root/js/app.js`, then you can acces that with `/js/app.js`. This will work even when you deploy your app somewhere else – Arkantos Mar 09 '15 at 17:32
  • Mind were not in WEB-INF either, I thought referring to them as '/' would fail if the servlet was not deployed as ROOT.war but it seems not, so thanks again for your help. – Paul Taylor Mar 10 '15 at 09:22
  • Sorry for not being clear. I was talking specifically about your case where in you're explicitly changing the context path of your application by renaming your war to ROOT.war. If that's not the case, you should always prefix your context path in your static files URL, something like `/myApp/js/app.js` – Arkantos Mar 10 '15 at 13:45
  • Ya it will work because you renamed your war to ROOT.war which will change the context path of your application to `/`.. so without any additional path segment, `/js/app.js` which is saying start from root directory, find js sub-directory, find app.js. Let's say your war name is `SampleWeb.war`, then tomcat's root application is running with `/` and you can access your app only with `/SampleWeb` context path, so your static file URL should be `/SampleWeb/js/app.js` which means find an application running on tomcat with `SampleWeb` as context path and then find `app.js` in js folder – Arkantos Mar 11 '15 at 10:13
0

The servlet mapping is specific. You need to add a wildcard.

<servlet-mapping>
    <servlet-name>WidgetServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
Will Hartung
  • 115,893
  • 19
  • 128
  • 203