redirect through web.xml in google-app-engine

梦想与她 提交于 2019-12-13 00:55:55

问题


After migrating to the new HRD store, I want to redirect requests to my old application to the new HRD application. I know I should let the Google migration tool make an alias, but since I migrate an intermediate copy of my app (because of the nightmares that migrating the database causes) that is not an option.

My plan was to use a servlet that does a HTTP 301 (HttpServletResponse.SC_MOVED_PERMANENTLY) redirect, and use a servlet-mapping with /* in web.xml. This works locally, but on the real app-engine, it doesn't. It seems like there is no URL pattern that the app-engine correctly recognizes. So far I have:

  <servlet-mapping>
    <servlet-name>RedirectToHRD</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RedirectToHRD</servlet-name>
    <url-pattern>/*</url-pattern>
    <url-pattern>/*/*</url-pattern>
    <url-pattern>/*/*/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RedirectToHRD</servlet-name>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RedirectToHRD</servlet-name>
    <url-pattern>index.jsp</url-pattern>
  </servlet-mapping>

I know it looks crazy, but I was desperate. Only '/' and 'index.jsp' get redirected, through the RedirectToHRD servlet. For other pages (JSP or anything else) this does not work. The log file just happily indicates that the pages get served.

Can anyone tell me what is happening?


Edit: I did what Peter Knego kindly suggested below, and made a filter. Now my web.xml has:

  <filter-mapping>
    <filter-name>RedirectToHRDFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

This still does not work on the 'real' appengine, and like the earlier attempt it does work locally. My filter has the following method:

  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain)
  throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest)req;
    HttpServletResponse response = (HttpServletResponse)resp;
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    String redirectURL = "http://fit20app-hrd.appspot.com"+request.getRequestURI();
    if (request.getQueryString() != null) {
      redirectURL += "?"+request.getQueryString();
    }
    response.setHeader("Location", redirectURL);
  }

Now I am thinking that this filter may be broken, even though it works locally. On Google's servers it works for / and /index.jsp, but not for anything else.


This is solved, see comment below.


回答1:


You can not have multiple <url-pattern> inside a <servlet-mapping>. Instead create multiple <servlet-mapping> with each having one <url-pattern> element.

Also, since you are trying to redirect everything you should use a servlet filter instead.



来源:https://stackoverflow.com/questions/13563455/redirect-through-web-xml-in-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!