2

I have a route() that fires when I go to http://localhost:8080/ and it's working, but it is blocking my resources from loading. e.g http://localhost:8080/mobile/css/styles.css returns 404

I used <url-pattern>/</url-pattern> in my web.xml and @Path("/") in my code. Any ideas how I can serve my static content while dynamically creating the index.html?

Thanks!

Edit - I want to avoid doing a redirect and keep my URL clean. If there is another framework that can do this better I'd be willing to try it. I'm not attached to JAX-RS.

package com.project.router;
@Path("/")
@Produces({MediaType.TEXT_HTML})
public class Router {

    final String FULL_WEB = "";
    final String MOBILE_WEB = "C:\\workspace\\project\\web\\mobile\\index.html";

    @GET
    @Produces({MediaType.TEXT_HTML})
    public Response route(@HeaderParam("user-agent") String userAgent, @HeaderParam("accept") String accept) throws FileNotFoundException {
        UAgentInfo uAgentInfo = new UAgentInfo(userAgent,accept);
        boolean tierIphone = uAgentInfo.detectTierIphone();
        File file = null;
        if (tierIphone) {
            file = new File(MOBILE_WEB);
        } else {
            file = new File(FULL_WEB);
        }

        FileInputStream fileInputStream = new FileInputStream(file);
        return Response.ok().entity(fileInputStream).build();
    }
}

web.xml

<servlet>
    <servlet-name>router</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.project.router</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>router</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
sissonb
  • 3,730
  • 4
  • 27
  • 54

1 Answers1

2

You should give a diffent url-pattern for your router servlet else all requests are going to reach your Router class. The static content will automatically work.

Maybe

<servlet-mapping>     
    <servlet-name>router</servlet-name>     
    <url-pattern>/router</url-pattern>
</servlet-mapping> 
basiljames
  • 4,777
  • 4
  • 24
  • 41