请求转发到 GWT 托管页面不起作用

Request forwarding to GWT hosted page not working

我在 html 中创建了一个登录页面,它向 servlet(LoginServlet) 提交表单。

身份验证成功后,我已将请求转发到 gwtPage.jsp,它会加载 nocache.js 脚本

方法一: 下面是我的 LoginServlet 代码

request.setAttribute("loginId",loginId);
dispatcher = request.getRequestDispatcher("gwtPage.jsp");
dispatcher.forward(request, response);

在我的 gwtPage 上,我包含了以下脚本

<script type="text/javascript" language="javascript" src="pc/pc.nocache.js?<%= new Date()%>"></script>

然而,从 LoginServlet 成功验证后,应用程序指向 gwtPage.jsp 但不加载 GWT 模块。 可能是因为认证后url显示:http://127.0.0.1:8888/LoginServlet

方法二: 我尝试了另一种方法,使用 response.sendRedirect 方法。

LoginServlet代码

response.sendRedirect("gwtPage.jsp?gwt.codesvr=127.0.0.1:9997");

它正确指向 http://127.0.0.1:8888/gwtPage.jsp?gwt.codesvr=127.0.0.1:9997 但是,我无法发送隐藏属性(我不想通过 URL 参数传递它)

请为我的任何一种方法提供一些建议。欢迎任何想法。

没有 "hidden attribute" 这样的东西 - 它很容易被提取和伪造。如果要使其安全,则需要使用会话。工作流程非常简单:

  1. 在您的 LoginServlet 中验证用户。将身份验证令牌(如您的 loginId)保存到会话中。

    HttpSession session = request.getSession(true);
    session.setAttribute("login", loginId);
    
  2. 将用户重定向到应用页面。

    response.sendRedirect("/gwtPage.jsp");
    
  3. 在此 JSP 从会话中检索身份验证令牌。如果不存在,则重定向回登录页面。如果存在,请继续加载应用程序。

    HttpSession session = request.getSession(true);
    if (session == null || session.getAttribute("login") == null) {
        response.sendRedirect("/Login.jsp");
    }
    

    确保您的服务器上启用了会话。