Note that there are some explanatory texts on larger screens.

plurals
  1. POpass array from jsp to servlet
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>By the way, im working on netBeans, with Tomcat.</p> <p>JSP code:</p> <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Dynatree - Example&lt;/title&gt; &lt;script src="jquery/jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="jquery/jquery-ui.custom.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="jquery/jquery.cookie.js" type="text/javascript"&gt;&lt;/script&gt; &lt;link href="src/skin/ui.dynatree.css" rel="stylesheet" type="text/css"&gt; &lt;script src="src/jquery.dynatree.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function myFunction(){ alert("Hello! I am an alert box!"); }; &lt;/script&gt; &lt;/head&gt; &lt;body class="example"&gt; &lt;h1&gt;Example: Form&lt;/h1&gt; &lt;form action="Test" method="post" name="form" &gt; Username: &lt;input type="text" name="userName" /&gt; &lt;br&gt; &lt;textarea name="comment"&gt;&lt;/textarea&gt; &lt;br&gt; &lt;input type="submit" id="id2" value="Send data"&gt; &lt;br&gt; &lt;input type="button" onclick="myFunction();" value="Show alert box"&gt; &lt;/form&gt; &lt;script type="text/javascript"&gt; $("#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!"); } }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And my servlet "Test" Code:</p> <pre><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() */ } </code></pre> <p>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.</p> <pre><code>Description:This is the description Print:null </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload