weixin_33717298 2016-02-10 17:49 采纳率: 0%
浏览 66

在jsp页面中使用postdata

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.

  • 写回答

2条回答 默认 最新

  • 撒拉嘿哟木头 2016-02-10 18:26
    关注

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

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛