简体   繁体   中英

How to redirect in web.xml

I'm working on a GAE/J app and I have the following structure:

- war
    |
    - mobile
    |      |
    |      - assets
    |      |      |
    |      |      + a.jpg
    |      |      + b.gif
    |      |      + c.js
    |      + a.jsp
    |      + b.jsp
    |      + index.jsp
    |      + d.css
    |      + e.css
    |
    + landing.jsp

Now in web.xml, I have a URL mapping of /mobile pointing to <jsp-file>/mobile/index.jsp<jsp-file> . In my index.jsp , I used relative addressing to reference all the required resources like JS files, CSS files and images; and this works well if I type our_domain/mobile/index.jsp in the browser. But when I type our_domain/mobile , it serves the correct file (index.jsp) but every relative address breaks which means no CSS, no JS, no image. From my investigation, it seems that web.xml doesn't actually redirect but rather calls a specified JSP/Servlet to answer to a specific URL pattern. This does not work well with relative addressing as path information that would be correct with a real redirect is left at the root. How do I ensure that relative addresses work as expected in this kind of scenario?

[edit] I've tried sevral configurations. Now /mobile URL works properly and all my CSS gets loaded but /mobile/ doesn't load my CSS. Here's my mapping:

<servlet> <description>Landing page on mobile site</description> 
 <display-name>MobileIndex</display-name>
 <servlet-name>MobileIndex</servlet-name>
 <jsp-file>/mobile/index.jsp</jsp-file> 
</servlet>

<servlet-mapping>
 <servlet-name>MobileIndex</servlet-name>
 <url-pattern>/mobile/</url-pattern>
</servlet-mapping> 

<servlet-mapping>
 <servlet-name>MobileIndex</servlet-name>
 <url-pattern>/mobile</url-pattern>
</servlet-mapping>

I'm not sure redirect is what you actually want. A good organization for your app is:

myapp/ pom.xml src/ main/ java/ ... Your Source Code ... webapp landing.jsp assets/ a.jpg b.gif c.js mobile/ index.jsp a.jsp b.jsp You can, of course, change the location of your assets using the <public-root> and <static-files> elements of appengine-web.xml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM