-2

i have requirement i.e., i need to pass array of javascript to servlet. please guide me thanks

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

Colin O'Dell
  • 8,386
  • 8
  • 38
  • 75
kumar
  • 148
  • 6
  • 25
  • Are you not using ajax?? – Stunner Apr 01 '14 at 09:06
  • no, i am not aware ajax.there is there any other way please let me know – kumar Apr 01 '14 at 09:09
  • possible duplicate of [How to transfer data structures between browser and server](http://stackoverflow.com/questions/10741743/how-to-transfer-data-structures-between-browser-and-server) – Djizeus Apr 01 '14 at 09:10
  • i need some example will u provide – kumar Apr 01 '14 at 09:12
  • Look at the example below or visit the site link for complete documentation from my answer below! – Ritikesh Apr 01 '14 at 09:18
  • @kumar, u can use [HttpServletRequest.getParameterValues](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterValues%28java.lang.String%29) to get array of parameters. can u share how are sending the parameters to servlet and wat is the servlet's url pattern in web.xml –  Apr 01 '14 at 09:29

3 Answers3

2

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).

Ritikesh
  • 1,228
  • 1
  • 9
  • 19
0

you can pass by using ajax

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

            },
            error: function (jqXHR) {
            }
        });
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

JSP page

<%@ 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
    }

}
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
  • i used ur code like this it gives null pointer exeception function getTotalTests()//this is onClick method of submit button { console.log("called"); console.log("called"+array1.length); for(i=0;i' + '' + ''); $(form).submit(); array2 = []; array1 = []; } ; – kumar Apr 01 '14 at 09:51
  • don't you think it'll be better for the network and application performance by just making an ajax request and handling it on the server side instead of making a complete one? – Ritikesh Apr 03 '14 at 14:43
  • @Ritikesh yes it is.. but read the question and comments, he did not want AJAX. – Deepu--Java Apr 04 '14 at 04:36
  • He said he's not aware of AJAX. IMHO, he can always learn new things and code better. :) – Ritikesh Apr 04 '14 at 19:04