【问题标题】:JSP: Error in forwarding pageJSP:转发页面出错
【发布时间】:2011-05-10 19:05:52
【问题描述】:

当我点击一个锚点时,这个问题与上一个问题有关

<a href="#" id="Email">send email</a>

它使用 json 调用 servlet

   $("#Email").click(function() {

    var option={
        "action":"sendEmail"
    };
    $.getJSON('StudentManagementServlet',option, function(hasEmail) {
        if(hasEmail == false){
            // //view form to let user enter his email
            $("#CommViaEmail").fadeIn("normal");   
        }
    });
});

在 servlet 中我处理请求

 if (action != null && action.equals("sendEmail")) {

    //open connection to db
    con.setAutoCommit(false);
    String email = ParentManagement.getParentEmail(con, stdNo);
    if (email != null) {
        String commResult = createAccountAndSendEmail(con, parentNo, email);
        request.setAttribute("result", commResult);
        request.setAttribute("incp", "ResultPage");
         RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
        dispatcher.forward(request, response);  //doesn't make forward!!!!!
        System.out.println(">>send email DONE!!");
        con.commit();
        return;
    } else {
        boolean hasEmail = false;
        String json = new Gson().toJson(hasEmail);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);

    }
}

这里的问题是如果用户有一封电子邮件,那么我发送一封电子邮件但请求不会转发到结果页面,即使打印语句被打印“System.out.println(”>>send email DONE!!” );" ??

【问题讨论】:

    标签: java json jsp servlets


    【解决方案1】:

    您需要让 JS/jQuery 来完成这项工作。让 servlet 将 true 写入 JSON 结果并在 JS 中执行

    if (hasEmail) {
        window.location = 'index.jsp';
    } else {
        $("#CommViaEmail").fadeIn("normal");   //view form to let user enter his email
    }
    

    或者当您想自己控制 URL 时,将新位置添加到 JSON 中

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("hasEmail", true);
    data.put("location", "index.jsp");
    // ...
    

    与

    ..., function(data) {
        if (data.hasEmail) {
            window.location = data.location;
        } else {
            $("#CommViaEmail").fadeIn("normal");   //view form to let user enter his email
        }
    }
    

    【讨论】:

    • @BalusC,我有很多属性要与请求一起转发,如何使用 javascript 转发它们?
    • 您可以将它们作为请求参数传递。毕竟,您为什么要转发到另一个页面?为什么不直接在同一页面中显示一些成功消息?
    • @BalusC,我的老师曾经告诉我,在 Web 应用程序中,结果会显示在另一个页面中。
    • @BalusC,如何将它们作为请求参数传递??
    • 1) 他们在 90 年代就是这样做的,是的。 2) 通过将它们作为请求参数添加到 URL。例如。 index.jsp?foo=bar&amp;waa=moo.
    【解决方案2】:

    您正在从客户端发出 AJAX 请求,并试图在服务器端“转发”该请求。

    AJAX 请求不要刷新页面。 javascript 函数 中的hasEmail 变量将是一个包含index.jsp 的HTML 的字符串。

    【讨论】:

    • hasEmail 变量有布尔变量而不是字符串,这个变量让我知道用户没有电子邮件,所以我显示他的发件人以添加他的电子邮件。
    • 只有当你的服务器端代码中的“else”块被执行时它才会是假的。否则,它将包含 index.jsp 中的 html 字符串。
    • 我找不到将 index.jsp 设置为 hasEmail 变量的位置!
    • 我指的是javascript函数中的hasEmail变量。 docs.jquery.com/GetJSON 回调函数的参数包含来自服务器的响应。在您的 Javascript 代码中恰好是 hasEmail。因此,如果“if”块在服务器端执行,则 JS 中的“hasEmail”将具有 JSP 的 HTML 字符串。如果“else”块被执行,它将是一个评估为 false 的 JSON 对象。希望这是有道理的。
    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    相关资源
    最近更新 更多