如何在glassfish中实现url重定向?

时间:2017-03-08 11:36:00

标签: glassfish url-redirection

我在Glassfish 3服务器上运行了一个应用程序。我需要将访问我的应用程序的任何用户重定向到另一个URL。对于ex- abc.def.com:8080/abc/index.html到xyz.def.com:8080/abc/index.html我尝试通过在我的Glassfish Configuration Server-config>中添加属性来实现。编辑虚拟服务器>添加属性名称= redirect_1 value =“from = abc.def.com:8080 / abc / index.html url-prefix = http://xyz.def.com:8080/abc/index.html”但它有效。 任何人都可以建议如何创建这样的重定向?或者还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

我认为为时已晚,但这可以帮助别人。 上周我遇到了同样的问题,但就我而言,所有服务器都被重定向了。在这种情况下,我只创建一个缓存服务器筛选器。在我的情况下,我需要过滤器上的各种项目,但只需要重定向,你只需要" SC_MOVED_PERMANENTLY"阻止......这是我的代码:

 @WebFilter("/faces/*")
public class CacheFilter implements Filter, Serializable {
  @Override
  public void init(FilterConfig config) throws ServletException{
    System.out.println("Cache Filter initated");
  }
  private boolean useFilter = false;
  private boolean useCache = false;
  @Override
  public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) throws IOException, ServletException{
    final HttpServletRequest request = (HttpServletRequest)req;
    final HttpServletResponse response = (HttpServletResponse)res;
    if(!useCache){
      response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
      response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
      response.setDateHeader("Expires", 0); // Proxies.
    }
    final HttpSession session = request.getSession(true);// don't create if it doesn't exist
    System.out.println("session is " + session);
    if(session!=null&&!session.isNew()){
      chain.doFilter(request, response);
    }else{
      String url = "/"+Server.AMBIENTE.getName()+"/faces/prod/index.xhtml";
      System.out.println("url: " + url);
      boolean redirect = false;
      System.out.println("requested url: " + request.getRequestURI());
      if(!request.getRequestURI().equals(url) && redirect){
        //System.out.println("SEND REDIRECT");
        //response.sendRedirect(url);
        System.out.println("SEND REDIRECT TO ANOTHER SITE");
        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        response.setHeader("Location", "http://www.google.com.br/");
      }else{
        chain.doFilter(request, response);
      }
    }
//        } catch (final Exception ex) {
//            response.sendRedirect(request.getContextPath() + "/server/v4/template/expirated.xhtml");
//        }catch(final Throwable ex){
//            System.out.println("");
//        }
//        }
  }
  @Override
  public void destroy(){
    // If you have assigned any expensive resources as field of
    // this Filter class, then you could clean/close them here.
  }
}