0

I have a form in my jsp page, and I have to capture the POST variables when clicking ok button and print them.

I think my solution is Ajax,but I'm not able to get it.

 <html>
 <head>
  <meta http-equiv="Content-Type" content="text/html"; charset="ISO-8859-1">
   <title>Ejemplo de carga de datos</title>
   <script src="/Scripts/jquery-1.9.1.js"></script> 

 <script>
     $(function(){ 
        $(document).ready(function () {
           $('input[type="submit"]').click(function (event) {

             var url = "datos.jsp"; // El script a dónde se realizará la petición.

    $.ajax({
       type: "POST",
       url: url,
       data: $("#form").serialize(), // Adjuntar los campos del formulario enviado.
       success: function(data)
       {

       }

     });


        });

    });
});

 </script>
</head>
    <body>


     <form name="form" id="form"  method="post" enctype="multipart/form-data" >
     <textarea cols=50 rows=10 name="texto" id="texto"></textarea>  

     <input type="submit" name="submit" value="Enviar datos" />

     </form>

 </body>
</html>

And datos.jsp:

  <%

      String texto = request.getParameter(texto);

      System.out.println(texto)

       %>

But it doesnt work How can I do it?

I add @Aleksey Bykov's sugestion.

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 <html>
  <head>
     <meta http-equiv="Content-Type" content="text/html"; charset="ISO-8859-1">
        <title>Ejemplo de carga de datos</title>

          <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

       <script>
    $(function(){ 
       $(document).ready(function () {
       $('input[type="submit"]').click(function (event) {



            var textoVar = $('#texto').val();
            alert(textoVar);
    $.ajax({
        url: 'page.jsp',
        type: 'POST',
   dataType: 'json',
        data: {
            texto: textoVar
        },
        success: function (data) {
            //
        },

        failure: function (data) {
            //
        }



    });

});
});
});
 </script>
</head>
<body>


    <span>Sent via AJAX: <b><c:out value="${param.texto}" /></b></span>


    <form name="form" id="form"  method="post" enctype="multipart/form-data" >
     <textarea cols=50 rows=10 name="texto" id="texto"></textarea>  

     <input type="submit" name="submit" value="Enviar datos" />


    </form>

</body>

It always prints ${param.texto} (this line not the value), even without send the form.

cucuru
  • 3,456
  • 8
  • 40
  • 74

2 Answers2

0

Do you have to use Javascript? This can be done by just adding an action to the form for skipping all the ajax stuff.

 <form name="form" id="form"  method="post" action="datos.jsp" >
      <textarea cols=50 rows=10 name="texto" id="texto"></textarea>  

      <input type="submit" name="submit" value="Enviar datos" />
 </form>
LAROmega
  • 488
  • 4
  • 13
  • no, I can't do this, my architecture is more complex, it isn't an usual server. Thanks anyway! – cucuru Feb 11 '16 at 07:53
0

You can get the value of the texto parameter by using JSTL:

<c:out value="${param.texto}" />

Full example - after clicking the submit button you will see at the top of the textarea the text that you entered.

<script>
    $("#submit").click(function() {
        var textoVar = $('#texto').val();
        $.ajax({
            url: 'YourJspHere',
            type: 'POST',
            dataType: 'json',
            data: {
                texto: textoVar
            },

            success: function (data) {
                //
            },

            failure: function (data) {
                //
            }
        });
    });
</script>

<span>Sent via AJAX: <b><c:out value="${param.texto}" /></b></span>
<form name="form" id="form"  method="post">
    <textarea cols=50 rows=10 name="texto" id="texto"></textarea>
    <input type="submit" name="submit" value="Enviar datos" />
</form>

Before sending:

enter image description here

After sending:

enter image description here

  • thanks, it's the way to get my objetive, but it doesn't work. Do I have to import some jquery library o something? – cucuru Feb 11 '16 at 08:56
  • I had the jquery imported, but not the tablib, now it always prints ${param.texto}, instead the value – cucuru Feb 11 '16 at 09:28
  • I've already imported de taglib, when printing always ${param.texto}. I update my original post with your help. I'm sure it's the good way! – cucuru Feb 11 '16 at 09:37