0

I have a JSP file in which there are two textareas and a submit button all in a form ;

<form action="" method="post">
        <textarea name="inConsole" id="in" cols="100" rows="10"> </textarea> <br/>
        <input type="submit" name="Send Command"/> <br/>
        <textarea name="outConsole" id="out" cols="100" rows="10"></textarea>
</form>

this page is supposed to work like any SQL program. so the user types a command in the first textarea and clicks submit, then the value of textarea will be extracted to a field and a method will take care of the command and return a log (1 row inserted, error:bad syntax etc) which will be displayed in the second textarea.

I know how for example make a login page and send data and redirect user to a new page(new jsp) file if user pass is correct.

what I can't find is how can I do all the things that I said above without going to a new page while using form action. I have checked other questions that linked the action attribute to a servlet which was confusing for me( the way that a servlet was called). I'm looking forward to use a simple scriptlet for this purpose like the one I used for my login page:

 <%
    DatabaseLoginTest dbLogTest = new DatabaseLoginTest();
    if (dbLogTest.DBLoginChecker(request.getParameter("user"), request.getParameter("pass")) == true){
        %>
         <p>Login Successful</p>
     <% } else { %>
        <p>Login Failed</p>
    <% } %>

also I'm aware that adding java scripts(not Javascript scripts:) ) to html isn't a good practice(and the reasons for it) but I think this might be easier for a simple program that I'm working on.

p.s: I'm using tomcat and Intellij for developing this web application and I have made a custom SQL so I only need the code that gives me the textarea value and the one that sets the other one's value

Update: now I know I should use javascript but I don't know how can I send the data extracted by javascript to a java method.

Vicarious
  • 131
  • 18
  • Why on earth is it not good practice to enhance HTML with JavaScript? Anyway, look into AJAX which is what you need – mplungjan Jul 19 '15 at 20:10
  • @mplungjan I was talking about this question https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files I said it wrong probably. I've taken a look at AJAX and java script but I don't know how to send the data that is extracted by javascript to a field , to the java method I have for processing SQL commands – Vicarious Jul 19 '15 at 20:14
  • 1
    JAVA is NOT JavaScript so yes you said it wrong :) Please search for AJAX JSP anywhere - for example http://stackoverflow.com/questions/8907383/a-simple-ajax-with-jsp-example – mplungjan Jul 19 '15 at 20:16
  • @mplungjan I meant java script not javascript scripts :) .I'm not very adept at technical terms. so could you add what you said as an answer ? – Vicarious Jul 19 '15 at 20:19
  • If my link helps you, delete the question and optionally open a new one with specific issues – mplungjan Jul 19 '15 at 20:22

1 Answers1

1

If you want to do this while remaining in the same page, you have to use Javascript. This is because if you want the server to be able to re-render the page, there has to be a page refresh.

You would need to write onClick handler for the submit button and make a Ajax call to your server to a specific URL with the user input. This URL would serve the data needed for the necessary UI changes.

You can use a scriptlet to generate the HTML that would be shown in the webpage but this would only suffice for a simple use-case and it would be a lot simpler if, say, your service returned just the data required to make the UI change and actual UI change is handled by the JS.

Also,I don't think it is a bad practice to embed JS in HTML. Sure, you can optimize this by including a JS source file but that's a separate optimization.

Nilanjan Basu
  • 828
  • 9
  • 20
  • thank you . could also tell how I can use that data that is extracted by javascript in a java method ? so the textarea value will be assigned to a variable in javascript then I should send it to a java method. – Vicarious Jul 19 '15 at 20:24
  • 1
    http://stackoverflow.com/questions/3985464/jquery-post-data-to-a-jsp-method?rq=1 This should give some idea. – Nilanjan Basu Jul 19 '15 at 20:30