JSP:转发页面时出错

发布于 2024-11-05 22:45:50 字数 1534 浏览 0 评论 0原文

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

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

它会使用servlet中的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");   
        }
    });
});

我处理请求

 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!!");" ??

This question is related to the previous one, when I click over an anchor

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

it calls servlet using json

   $("#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");   
        }
    });
});

in servlet I handle the request

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

    }
}

The problem here is if user has an email, then I send an email but request dosn't forward to result page, even the print statement is printed " System.out.println(">>send email DONE!!");" ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北恋 2024-11-12 22:45:50

你需要让 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
    }
}

You need to let JS/jQuery do that job. Let the servlet write true as JSON result and in JS do

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

Or when you want to control the URL yourself, add the new location to the JSON

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

with

..., function(data) {
    if (data.hasEmail) {
        window.location = data.location;
    } else {
        $("#CommViaEmail").fadeIn("normal");   //view form to let user enter his email
    }
}
寂寞笑我太脆弱 2024-11-12 22:45:50

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

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

You are making an AJAX request from the client and are trying to 'forward' that request in the server side.

AJAX requests DONT refresh the page. The hasEmail variable in javascript function will be a string containing the HTML of the index.jsp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文