Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.xml and/or Filters to return welcome-file

I have a need to configure my Tomcat WAR for specific functionality, and not sure if it can all be accomplished via web.xml, or if I need to implement 1+ custom Filters, or use some other type of hackery.

My app packages up as myapp.war. So when it's served from a local Tomcat instance, I can access it by going to http://localhost:8080/myapp.

Very simply, I have a welcome-file (myapp.html) that I want served if Tomcat receives the following requests:

  • localhost:8080/myapp
  • localhost:8080/myapp/
  • localhost:8080/myapp/#
  • localhost:8080/myapp/#<blah>

...where <blah> is any string/regex after the pound symbol (#).

So if the user goes to http://localhost:8080/myapp, then serve back myapp.html. If the user goes to http://localhost:8080/myapp/#fjrifjri, then guess what? Serve back myapp.html.

But, if the user goes to, say, http://localhost:8080/myapp/fizz, then I want normal web.xml servlet-mapping logic to kick in, and I want Tomcat to serve back whatever servlet is mapped to /fizz, etc.

Currently my web.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <welcome-file-list>
        <welcome-file>myapp.html</welcome-file>
    </welcome-file-list>
</web-app>

How can I accomplish this?

like image 500
IAmYourFaja Avatar asked Dec 27 '13 21:12

IAmYourFaja


2 Answers

If you need to mess around with URLs you need to use servlet and servlet-mapping tags:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <welcome-file-list>
    <welcome-file>myapp.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>fizz</servlet-name>
    <servlet-class>demo.fizz</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>fizz</servlet-name>
    <url-pattern>/fizz</url-pattern>
  </servlet-mapping>
</web-app>

where demo is your package and fizz your fizz.java
To modify url and files attached to the current url you need to use servlet-mapping tags where servlet called fizz is mapped to /fizz

This will allow you to change settings you are looking for.

Hope it helps...

like image 140
Andrew_Dublin Avatar answered Nov 11 '22 23:11

Andrew_Dublin


One way to accomplish this is using filters is as follows:

  • Create a filter which is mapped to root / in web.xmlas follows

    <filter>
      <filter-name>myWelcomeFilter</filter-name>
      <filter-class>com.test.MyWelcomeFilter</filter-class>
    </filter> 
    
  • and the filter mapping as follows

<filter-mapping>
  <filter-name>myWelcomeFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
  • in the doFilter() method use the following code.
 String pathInfo = request.getPathInfo();
 if(null == pathInfo || pathInfo.startsWith("#") ){
 response.sendRedirect("/myapp/myapp.html")
 }else{
  chain.doFilter(request, response);
 }
like image 34
Santosh Avatar answered Nov 11 '22 22:11

Santosh