【问题标题】:how to pass the javascript array to servlet如何将javascript数组传递给servlet
【发布时间】:2014-04-01 09:05:43
【问题描述】:

我有要求,即我需要将 javascript 数组传递给 servlet。请指导我谢谢

qwe.js

<script type="text/javascript">
    var array2 = [];
    function getTotalTests() { 
        console.log("called");
        console.log("called"+array1.length);
        for (i=0; i < array1.length; i++) {
            array2[i] = array1[i];
            console.log(array2[i]);
        }
    };
</script>

我需要将 array2 传递给 servlet

【问题讨论】:

标签: java javascript servlets


【解决方案1】:

您需要提出某种请求才能实现此目的。如果您不想发出完整的请求,可以查看https://api.jquery.com/jQuery.ajax/ 发出异步请求并显示所做的更改(如果需要)。

【讨论】:

    【解决方案2】:

    JSP 页面

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
     function func()
     {
         var arr=[2,3,3];
         var form = $('<form action="Test" method="get">' +
                 '<input type="hidden" name="id" value="'+arr+'">' +
                 '</form>');
         alert( $(form));
                 $(form).submit();
     }
    </script>
    <body>
        <button onclick="func()">Deepak</button>
    </body>
    </html>
    

    小服务程序

    package test;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Test
     */
    @WebServlet("/Test")
    public class Test extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * Default constructor. 
         */
        public Test() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            System.out.println(request.getParameter("id"));
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        }
    
    }
    

    【讨论】:

    • 我使用了这样的代码,它给出了空指针异常函数 getTotalTests()//这是提交按钮的 onClick 方法 { console.log("call"); console.log("调用"+array1.length); for(i=0;i' + '' + ''); $(form).submit();数组2 = [];数组1 = []; } ;
    • 你不认为只发出一个 ajax 请求并在服务器端处理它而不是一个完整的请求会更好地提高网络和应用程序的性能吗?
    • @Ritikesh 是的。但是阅读问题和 cmets,他不想要 AJAX。
    • 他说他不了解 AJAX。恕我直言,他总是可以更好地学习新事物和编码。 :)
    【解决方案3】:

    你可以通过使用ajax来传递

    $.ajax({
                type: "POST",
                url: "servletname", //Your full URL goes here
                data: {
                    dataname: datavalue
                },
                success: function (data, textStatus, jqXHR) {
    
                },
                error: function (jqXHR) {
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2012-12-15
      • 2012-09-24
      • 2014-09-21
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 2017-05-23
      相关资源
      最近更新 更多