【问题标题】:Ajax response doesn't display in JSP pageAjax 响应不显示在 JSP 页面中
【发布时间】:2015-06-09 06:22:10
【问题描述】:

我正在尝试使用 JSP ajax 技术来保存小记录。项目工作流程是这样的。

01. index.jsp : 发送数据到 SaveStudent servlet
02. SaveStudent : 获取请求表单jsp并将其发送到验证java类
03. 验证:验证数据并发送到 DaoImpl java 类
04. DaoImpl : 覆盖StudentDAO中的方法,进行保存SQL查询。
05. StudentDAO : 一个接口包含所有数据库相关的方法。


这是该项目的图像。


下面给出的是 index.jsp 文件的源代码。

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
 <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>


</head>
<body>
    <form action="SaveStudent" method="post">
        <label>Enter Name:</label>
        <input type="text" name="name" id="txtName"/>
        <br/>
        <label>Enter City:</label>
        <input type="text" name="city" id="txtCity"/>
        <br/>
        <input type="submit" value="Send" id="btnSave"/>
        <div id="response"></div>
    </form>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#btnSave').click(function() {
                var $name = $("#txtName").val();
                var $city = $("#txtCity").val();
                $.post('SaveStudent', {
                    name: $name,
                    city: $city
                }, function(responseText) {
                    if (responseText !== null) {
                        $('#response').text(responseText);
                    } else {
                        alert("Invalid Name");
                    }
                });
            });
        });

    </script>


</body>


这是 SaveStudent java 类的源代码。

 package Control;

 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import Model.Validation;

public class SaveStudent extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    String record = "";
    try {

        Validation val = new Validation();
        record = val.validateSave(request, response);
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        if (record != null) {
            out.write(record);

        } else {
            out.print("Error Occured..!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.close();
    }
}

}


保存记录工作正常。数据库也在更新。但问题是“保存成功”消息出现在 servlet 页面中。不在jsp页面下。


请帮助我。谢谢。

【问题讨论】:

  • 是的,它也有效。谢谢。我想出另一种方法。我将“提交”类型替换为“按钮”。现在工作完美。谢谢你的时间。
  • 是的,亲爱的,它正在工作。

标签: java jquery ajax jsp servlets


【解决方案1】:

我想出了一个办法。我将“提交”类型替换为“按钮”。现在工作完美。谢谢你的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多