chebeche
chebeche

Reputation: 11

pass array from jsp to servlet

im pretty a newbie in JSP and servlets, but i have been doing a lot of research and tried a lot but i couldnt make it work. I'm working with a servlet and a JSP. Im doing a kind of sign in, in which i need to send some form information, and an array (in this case i have created the array, but i get one similar from a Dynatree) to a servlet, manage the information, and then go on to another page.

I have tried many things, but nothings seems to work. May be im doing a lot of things wrong, but i dont have more ideas to test that might work.

By the way, im working on netBeans, with Tomcat.

JSP code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  <title>Dynatree - Example</title>

  <script src="jquery/jquery.js" type="text/javascript"></script>
  <script src="jquery/jquery-ui.custom.js" type="text/javascript"></script>
  <script src="jquery/jquery.cookie.js" type="text/javascript"></script>
  <link href="src/skin/ui.dynatree.css" rel="stylesheet" type="text/css">
  <script src="src/jquery.dynatree.js" type="text/javascript"></script>

    <script type="text/javascript">

      function myFunction(){
             alert("Hello! I am an alert box!");
            };
    </script>
</head>

<body class="example">
  <h1>Example: Form</h1>

  <form action="Test" method="post" name="form" >
    Username: <input type="text" name="userName" />
    <br>
    <textarea name="comment"></textarea>
    <br>
    <input type="submit" id="id2" value="Send data">
    <br>
    <input type="button" onclick="myFunction();" value="Show alert box">
  </form>   

   <script type="text/javascript">      
        $("#id2").click(function() {
                var json=[1,2,3,4];
            $.ajax({
                url:"Test",
                type:"POST",
                dataType:'json',
                data: {json:json},
                success:function(data){
                     alert("Hello! I am an alert box!");
                }
               });
        });
    </script>
</body>
</html>

And my servlet "Test" Code:

@WebServlet(urlPatterns = {"/Test"})
public class Test extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {         

        PrintWriter out = response.getWriter();            
        String descr = request.getParameter("comment");            
        String[] myJsonData = request.getParameterValues("json[]");

        out.print("\nDescription:");
        out.print(descr);
        out.print("\nPrint:");            
        out.print(myJsonData);

       // response.sendRedirect("pasoServlet.jsp");
    }
} /* end doPost() */    
}

My main problem is that when i try to print "myJsonData", prints null, and i think it shouldnt, but i dont know why it does.

Description:This is the description 
Print:null

Upvotes: 1

Views: 5896

Answers (1)

Gaurav Singla
Gaurav Singla

Reputation: 1451

try changing

String[] myJsonData = request.getParameterValues("json[]");

to

String[] myJsonData = request.getParameterValues("json");

Upvotes: 1

Related Questions