You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CKEditor数据提交成功但未写入MSSQL数据库问题排查

问题:CKEditor数据通过AJAX提交到Servlet后未写入MSSQL数据库

我正在开发一款Web应用,使用CKEditor实现用户输入试题及选择题选项的功能,尝试通过Java Servlet将数据保存至MSSQL数据库。

出现的问题

  • 前端通过AJAX请求发送数据
  • Servlet处理请求后返回"Data saved successfully!"提示
  • 但查看数据库时,数据并未实际保存

Servlet代码

@WebServlet("/SubmitExamServlet")
public class SubmitExamServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String examTitle = request.getParameter("title");
        String subject = request.getParameter("subject");
        String question = request.getParameter("question");
        String option1 = request.getParameter("option1");
        String option2 = request.getParameter("option2");
        String option3 = request.getParameter("option3");
        String option4 = request.getParameter("option4");
        int correctOption = Integer.parseInt(request.getParameter("correctOption"));

        String DB_URL = "jdbc:sqlserver://THE_NOOB_BANANA;Database=ExamPortal;encrypt=false;integratedSecurity=true";

        
        try {
            Connection conn = DriverManager.getConnection(DB_URL);
            String sql = "INSERT INTO ExamQuestions (ExamTitle, Subject, QuestionText, Option1, Option2, Option3, Option4, CorrectOption) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, examTitle);
            stmt.setString(2, subject);
            stmt.setString(3, question);
            stmt.setString(4, option1);
            stmt.setString(5, option2);
            stmt.setString(6, option3);
            stmt.setString(7, option4);
            stmt.setInt(8, correctOption);

            stmt.executeUpdate();
            conn.close();

            response.sendRedirect("success.jsp");
        } catch (SQLException e) {
            e.printStackTrace();
            response.sendRedirect("error.jsp");
        }
    }
}

JSP脚本代码

<script>
    function initializeCKEditors() {
        var editorConfig = {
            extraPlugins: 'mathjax',
            mathJaxLib: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML'
        };

        function suppressNotification(editor) {
            editor.on('instanceReady', function () {
                editor.showNotification = function (message, type) {
                    if (message.id !== 'editor-is-not-secure') {
                        return this.showNotification.original.call(this, message, type);
                    }
                };
                editor.showNotification.original = editor.showNotification;
            });
        }

        var questionEditor = CKEDITOR.replace('question', editorConfig);
        suppressNotification(questionEditor);

        var option1Editor = CKEDITOR.replace('option1', editorConfig);
        suppressNotification(option1Editor);

        var option2Editor = CKEDITOR.replace('option2', editorConfig);
        suppressNotification(option2Editor);

        var option3Editor = CKEDITOR.replace('option3', editorConfig);
        suppressNotification(option3Editor);

        var option4Editor = CKEDITOR.replace('option4', editorConfig);
        suppressNotification(option4Editor);
    }

    function sendDataToServlet() {
        // Collecting the correct option value
        var correctOption = $("input[name='correctOption']:checked").val();

        $.ajax({
            type: 'POST',
            url: 'SubmitExamServlet',
            data: {
                question: CKEDITOR.instances.question.getData(),
                option1: CKEDITOR.instances.option1.getData(),
                option2: CKEDITOR.instances.option2.getData(),
                option3: CKEDITOR.instances.option3.getData(),
                option4: CKEDITOR.instances.option4.getData(),
                correctOption: correctOption
            },
            success: function(response) {
                alert('Data saved successfully!');
            },
            error: function() {
                alert('An error occurred while saving the data.');
            }
        });
    }

    document.addEventListener('DOMContentLoaded', function() {
        initializeCKEditors();

        // Bind the AJAX submission to the publish button
        document.querySelector('.publish-btn').addEventListener('click', function(event) {
            event.preventDefault();
            sendDataToServlet();
        });
    });
</script>

疑问

我是否未正确从CKEditor获取数据?

已尝试/排查的内容

  • 数据库连接字符串及凭证正确
  • Servlet未捕获到任何异常或错误

内容的提问来源于stack exchange,提问作者COSYBANANA63

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:05:54