首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSP按条件重定向问题

JSP按条件重定向问题
EN

Stack Overflow用户
提问于 2012-11-12 22:52:05
回答 2查看 143关注 0票数 0

我有不同的俱乐部,不同的域名如下:

代码语言:javascript
复制
Kid club = kidclub.google.mobi
Youth Club = youthclub.yahoo.mobi
Adult Club=adult.godaddy.mobi
Elderly Club = elderly.google.mobi

重定向设置如下(重定向设置示例):

代码语言:javascript
复制
1. Kid Club should redirect to Youth Club 
2. Youth Club should redirect to Adult Club
3. Adult Club should redirect to Elderly Club
4. Elderly Club should redirect to Kid club

问题场景:如果用户试图订阅儿童俱乐部,那么,如果他没有注册到这个俱乐部,他会被转发到域名"kidclub.google.mobi“。但是如果他已经注册了,那么他应该被重定向到设置中定义的另一个俱乐部青年俱乐部(youthclub.yahoo.mobi)。如果他再次注册到青年俱乐部,那么他应该被自动重定向到成人俱乐部(adult.godaddy.mobi)。这一直持续到他没有注册的俱乐部。

我有以下代码,只可以重定向到一个单一的俱乐部,但我不能检查的条件,如果用户订阅了第二个俱乐部或没有。

代码语言:javascript
复制
//ClubDao.isActive(user,club) returns TRUE if user is active to that club and 
FALSE if user is inactive. 

if( user != null && club != null && ClubDao.isActive(user, club))
{
redirectReturningUser( request, response,domain );
}

void redirectReturningUser( HttpServletRequest request, HttpServletResponse 
response,Domain currentDomain )
{
String redirectToUrl = currentDomain.getDefaultUrl();

if( "kidclub.google.mobi".equals( currentDomain.getDefaultUrl() ) )
   redirectToUrl = "youthclub.yahoo.mobi";
else if( "youthclub.yahoo.mobi".equals( currentDomain.getDefaultUrl() ) )
   redirectToUrl = "adult.godaddy.mobi";
else if( "adult.godaddy.mobi".equals( currentDomain.getDefaultUrl() ) )
   redirectToUrl = "elderly.google.mobi";
else if( "elderly.google.mobi".equals( currentDomain.getDefaultUrl() ) )
   redirectToUrl = "kidclub.google.mobi";

       doRedirect(response, "http://"+redirectToUrl );
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-06 20:15:11

我使用while循环来解决这个问题,它成功了!

代码语言:javascript
复制
while(registered==true){

   //code for redirectconditions
    .......................

    checkifRegistered==false??
     }
  redirectUser(club URLLink);

谢谢,

票数 0
EN

Stack Overflow用户

发布于 2012-11-12 23:56:58

如果将每个Club映射到url字符串,然后在建立俱乐部的用户成员资格时检索该字符串,会怎么样呢?

代码语言:javascript
复制
  static Map<Club, String> redirectMap= new LinkedHashMap<Club, String>();

  static  {
    redirectMap.put(new Club("kidclub.google.mobi"), "youthclub.yahoo.mobi");
    redirectMap.put(new Club("youthclub.yahoo.mobi"), "adult.godaddy.mobi");
    redirectMap.put(new Club("adult.godaddy.mobi"), "elderly.google.mobi");
    redirectMap.put(new Club("elderly.google.mobi"), "kidclub.google.mobi");

  }

  void redirectReturningUser (HttpServletRequest request, HttpServletResponse response) throws IOException {
    Map<Club, String> tmpRredirectMap = new LinkedHashMap<Club, String>();
    for (Map.Entry<Club, String> entry: redirectMap.entrySet()){

      Club club = entry.getKey();
      String redirectUrl = entry.getValue();
      if( user != null && club != null && ClubDao.isActive(user, club))  {
        tmpRredirectMap.put(club, redirectUrl);
      }
    }
    boolean done = false;
    String tempUrl = domain.getDefaultUrl();
    int count = redirectMap.size();
    Club tempClub = new Club(tempUrl);
    do {
      if (tmpRredirectMap.keySet().contains(tempClub)){
        tempClub = new Club(redirectMap.get(tempClub));
        count--;
      } else {
        done = true;
      }
    } while (! done && count > 0);
    redirectReturningUser(request, response, tempClub.getName());
  }
  void redirectReturningUser( HttpServletRequest request, HttpServletResponse  response, String redirectUrl ) throws IOException {

    doRedirect(response, "http://"+redirectUrl );
  }

  private void doRedirect(HttpServletResponse response, String s) throws IOException {
    response.sendRedirect(s);
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13345864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档