gpt4 book ai didi

java - JSP重定向到不同的URL

转载 作者:行者123 更新时间:2023-11-30 04:36:45 24 4
gpt4 key购买 nike

在我的应用程序中,用户可以订阅不同的俱乐部,例如 child 俱乐部、青年俱乐部、成人俱乐部和老年人俱乐部。现在,假设如果用户已经在 2012 年 10 月 7 日订阅了 child 俱乐部,并且如果他在 2012 年 11 月 2 日再次尝试订阅同一俱乐部“ child 俱乐部”,那么我们会尝试将该用户重定向到另一个俱乐部,例如青年俱乐部或成人俱乐部或老年俱乐部。每个俱乐部都有自己的域名,例如(例如)

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

以下是重定向的示例代码,但这不是重定向到不同的域 URL。是因为sendRedirect()方法吗?这个应用程序中没有使用Servlet。 Apache Tomcat 服务器中仅使用 JSP 和 MySQL 数据库,请尽快提出建议。

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

提前致谢

最佳答案

response.sendRedirect(url); 适用于相对(不带 http:)和绝对 URL,因此问题一定出在其他地方。

最常见的问题原因是其他一些代码已经开始使用 getOutputStream()getWriter() 将输出写入响应:一旦第一个byte 以这种方式写入,将生成 HTTP header (其中包含重定向信息)并将其发送到浏览器。

因此您应该查看catalina.out,因为可能调用了e.printStackTrace()

您可能还想检查 currentDomain.getDefaultUrl() 返回的内容;也许上面的 if 根本不匹配。

关于java - JSP重定向到不同的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13310593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com