Reputation: 684
In my application, user can subscribe to different clubs like for example Kid club, Youth club, Adult club and Elderly club. Now, suppose if user is already subscribed to Kid club on 7th October 2012 and if he again tries to subscribe the same club "Kid Club" on 2nd November 2012 then we try to redirect that user to another club such as Youth Club OR Adult Club OR Elderly Club. Each club has it's own domain such as (For example)
Kid club = kidclub.google.mobi
Youth Club = youthclub.yahoo.mobi
Adult Club=adult.godaddy.mobi
Elderly Club = elderly.google.mobi
Following is the sample code for redirection but this is not redirecting to different domain URL. Is it due to sendRedirect () method?Servlet is not used in this application. Only JSP is used with MySQL database in Apache Tomcat Server Please suggest asap.
void doRedirect(HttpServletResponse response, String url)
{
try
{
response.sendRedirect(url);
}
catch (Exception e)
{
e.printStackTrace()
}
}
void redirectReturningUser( HttpServletRequest request, HttpServletResponse
response, ClubDomain currentDomain )
{
String redirectToUrl = currentDomain.getDefaultUrl();
if( "kidclub.google.mobi".equals( currentDomain.getDefaultUrl() ) )
redirectToUrl = "youthclub.yahoo.mobi";
else if( "adult.godaddy.mobi".equals( currentDomain.getDefaultUrl() ) )
redirectToUrl = "kidclub.google.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 = "adult.godaddy.mobi";
doRedirect(response, "http://"+redirectToUrl );
}
Thanks in advance
Upvotes: 1
Views: 9240
Reputation: 328594
response.sendRedirect(url);
works with relative (without http:
) and absolute URLs, so the problem must be elsewhere.
The most common cause of problems is that some other code already started to write output to the response using getOutputStream()
or getWriter()
: As soon as the first byte is written that way, the HTTP header (which contains the redirect information) will be generated and send to the browser.
So you should look into catalina.out
because the e.printStackTrace()
was probably called.
You might also want to check what currentDomain.getDefaultUrl()
returns; maybe the if
s above simply never match.
Upvotes: 2