简体   繁体   English

如何将javascript数组传递给servlet

[英]how to pass the javascript array to servlet

i have requirement ie, i need to pass array of javascript to servlet.我有要求,即,我需要将 javascript 数组传递给 servlet。 please guide me thanks请指导我谢谢

qwe.js 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>

i need pass array2 to servlet我需要将 array2 传递给 servlet

You will need to make a request of some sort to achieve this.您需要提出某种请求才能实现此目的。 If you do not wish to make a complete request, you can look at https://api.jquery.com/jQuery.ajax/ to make an asynchronous request and display the changes made(if required).如果您不想发出完整的请求,可以查看https://api.jquery.com/jQuery.ajax/以发出异步请求并显示所做的更改(如果需要)。

you can pass by using ajax你可以通过使用ajax

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

            },
            error: function (jqXHR) {
            }
        });

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

Servlet小服务程序

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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM