2

I am trying to hand over a text from a textarea. After serveral actions I want to return the result and print the result in another textarea.

First my index.jsp: The first textarea codeEditor has the text. Aftler clicking the button analysisButton the 2nd textarea commentBox should fill.

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

Then my index.js: Here I am trying to use AJAX like in the first answer of this question

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

and at least my servlet where I call my 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);
}

I am getting a NullPointerException while I am trying to set the variable sql in my MainVisitor

EDITED: I think my problem is that I didn't read the content of codeEditor

I added now var sql = $("#codeEditor").val(); to my JS but I don't have an idea how to proceed

Community
  • 1
  • 1
xNappy
  • 158
  • 1
  • 4
  • 12

3 Answers3

1

It will be nice though if you can provide the whole error logs... But I think your issue is just about the timing. Please do not forget that AJAX stands for Asynchronous JavaScript and XML.

This is important to understand because in asynchronous scheme, the next set of commands / lines get executed already even though you are still waiting for something to be returned. You should consider a callback function. You should put all the instructions that you think should wait for a certain entity to comeback before being processed.

If you have extra time, you might want to read this.

ecastro
  • 11
  • 2
  • this is a nice thing thank you too. This was my second problem because my textarea getting filled asynchron too. – xNappy Feb 26 '16 at 14:00
1

Try actually sending the data

$.get("AnalysisServlet", {codeEditor: $("#codeEditor").val()}, function(responseText){
    $("#commentBox").text(responseText);
});
Musa
  • 96,336
  • 17
  • 118
  • 137
  • this looks good. I don't get an exception anymore but the value is empty --> `""` empty String – xNappy Feb 25 '16 at 14:38
  • try `{codeEditor: 'select * from world'}` as the data parameter to see if you get anything – Musa Feb 25 '16 at 14:46
0

You need a form wrapping your textarea codeEditor .

Eugênio
  • 11
  • 4