我正在尝试从文本区域移交文本。经过服务器操作后,我想返回结果并将结果打印在另一个文本区域中。

首先是我的index.jsp:第一个textarea codeEditor具有文本。单击按钮analysisButton后,第二个文本区域commentBox应该会填充。

<div class="form-group">
    <label for="codeEditor" style="margin-top: 15px;">Code:</label>
    <textarea name="codeEditor" class="form-control" id="codeEditor" rows="15" style="resize: none;"></textarea>
</div>

<button id="analysisButton" class="btn btn-default btn-block" style="margin-top: 10px;">Start analysis</button>

<div class="form-group">
    <label for="comment" style="margin-top: 15px;">Comment:</label>
    <textarea class="form-control" id="commentBox" style="resize: none;height:330px;"></textarea>
</div>


然后是我的index.js:在这里,我试图像在question的第一个答案中一样使用AJAX

$('body').on('click', '#analysisButton', function(){
    $.get("AnalysisServlet",function(responseText){
        $("#commentBox").text(responseText);
    });
});


至少我的servlet叫我的bean

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
    MainVisitor mainVisitor = new MainVisitor();
    request.setAttribute("mainVisitor", mainVisitor);

    mainVisitor.setSql(request.getParameter("codeEditor"));

    String result = mainVisitor.getResult();
    resp.setContentType("text/html");
    resp.setCharacterEncoding("UTF-8");
    resp.getWriter().write(result);
}


我在尝试在我的sql中设置变量MainVisitor时遇到NullPointerException

编辑:
我认为我的问题是我没有阅读codeEditor的内容

我现在在我的JS中添加了var sql = $("#codeEditor").val();,但我不知道如何进行

最佳答案

尝试实际发送数据

$.get("AnalysisServlet", {codeEditor: $("#codeEditor").val()}, function(responseText){
    $("#commentBox").text(responseText);
});

09-07 13:01