4

JSP Redirection Issue according to Condition

 3 years ago
source link: https://www.codesd.com/item/jsp-redirection-issue-according-to-condition.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JSP Redirection Issue according to Condition

advertisements

I have different clubs with different domains as below :

Kid club = kidclub.google.mobi
Youth Club = youthclub.yahoo.mobi
Adult Club=adult.godaddy.mobi
Elderly Club = elderly.google.mobi

Redirection Settings are as below (sample redirection settings):

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

Problem Scenario: If user tries to subscribe Kid Club then, if he is not registered to this club, he is forwarded to domain "kidclub.google.mobi". But if he is already registered then he should be redirected to another club Youth Club (youthclub.yahoo.mobi) AS defined in the settings. And if he is again already registered to Youth Club then he should be automatically redirected to Adult Club (adult.godaddy.mobi). This goes on until to the club he is not registered with.

I have the following code which just can redirect to a single club but I am not able to check the condition if the user is subscribed to second club or not.

//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 );
}


What if you map each Club to the url string and then retrieve the string when the user membership against the club is established.

  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);
  }


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK