2011-09-06 66 views
1

我在这里使用的是谷歌应用程序引擎。使用JSP,除了请求管理页面之外,如何将所有内容重定向到保留页面

在web.xml中我有安全设置为这样:

<security-constraint> 
     <web-resource-collection> 
     <url-pattern>/admin/*</url-pattern> 
     </web-resource-collection> 
     <auth-constraint> 
     <role-name>admin</role-name> 
     </auth-constraint> 
    </security-constraint> 

现在我想通过/管理员使用servlet来使得一些大的变化到数据存储的架构,而重定向所有其他请求像BeBackSoon.jsp

有没有简单的方法来做到这一点与web.xml?

+0

您应该在进行更改后重新部署应用程序。也许我不明白你的问题? – home

回答

0

您可以使用filter

@Override 
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
    HttpServletRequest request = (HttpServletRequest) req; 
    HttpServletResponse response = (HttpServletResponse) res; 
    String contextPath = request.getContextPath(); 

    if (request.getRequestURI().startsWith(contextPath + "/admin") { 
     chain.doFilter(req, res); 
    } else { 
     response.sendRedirect(contextPath + "/BeBackSoon.jsp"); 
    } 
} 

将其映射到网址格式/*。请注意,如果您的CSS/JS /图片等静态资源位于不同的路径后面,则您希望在条件中包含像"/static"这样的常见路径的检查,否则您的管理页面将没有合适的CSS/JS /图像。

+0

两个(很容易修复的)问题:1)代码导致无限循环(检查请求是否为contextPath + destination 2)在GAE上,您还需要检查contextPath +“/ _ah /”,以处理管理员登录。 – MontyGomery

相关问题