2

my problem i am not get array from jquery post to jspservlet is shows nullpointer exception always.my jsp page code is listed below :

<!DOCTYPE html>
<html>
<head>
<script src="JS/jquery-latest.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
      $("p").fadeToggle(3000,function(){
          var fields=new Array();

          fields = $(":input").serializeArray();
          $.post("exampleservlet",{arraydata:fields,mode:"Insert"},
          function(data) {
                alert("Data Loaded: " + data);
          });      
      });
      return false;
  });
});
</script>
</head>
<body>
    <form>
        <input type="text" name="txt_name" />
        <input type="text" name="txt_name1" />
        <input type="text" name="txt_name2" />
        <input type="text" name="txt_name3" />
      <p style="background:blue;color:white;padding:10px;">If you click on me, I will disappear.</p>
        <button>Click me</button>
    </form>   
</body>
</html>

and my jsp servlet java code listed below:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String jsresponse="No Message";

       try
        {
        String arrayData[] = request.getParameterValues("arraydata");
        //String data[]=request.getParameterValues("arraydata[]");

        jsresponse = arrayData[0] +" - ";//"This line shows null pointer exception!";

        }
        catch(Exception ex)
        {

        }


        response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
        response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
        response.getWriter().write(jsresponse);
    }

and i am getting null pointer exception on this line

jsresponse = arrayData[0];//"This line shows null pointer exception!";

it's shows error log like below:

Nov 15, 2012 4:10:03 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [exampleservlet] in context with path [/javaservletwithajax] threw exception
java.lang.NullPointerException
    at com.example.servlets.exampleservlet.doPost(exampleservlet.java:56)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

any one help with me how to get array from dopost parameter values in jsp servlet!

Solved My Problem from kmb385 like below:

Changes in jquery

$(document).ready(function(){
  $("button").click(function(){
      $("p").fadeToggle(3000,function(){

           var fields=new Array();
           var values = new Array();

           fields = $(":input").serializeArray();
           $.each(fields, function(index,element){
             values.push(element.value);
           });

          $.post("exampleservlet",{arraydata:values,mode:"Insert"},
          function(data) {
                alert("Data Loaded: " + data);
          });

      });
      return false;
  });
});

Changes in jsp servlet:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String jsresponse="No Message";

   try
    {
    String arrayData[] = request.getParameterValues("arraydata[]");
    //String data[]=request.getParameterValues("arraydata[]");
    jsresponse = arrayData[0] +" - ";//"Test Response!";

    }
    catch(Exception ex)
    {
     jsresponse = ex.toString();               
    }
    response.setContentType("text/plain");  // Set content type of the response so that            jQuery knows what it can expect.
    response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
    response.getWriter().write(jsresponse);
}

it's works cool!

1
  • any luck getting this to work? Commented Nov 15, 2012 at 11:11

1 Answer 1

3

There are two issues being encountered.

  1. Brackets must be specified in the String literal used to retrieve the parameter values.
  2. An array is not being passed over the wire to the servlet.

To solve the first issue, change the argument passed to getParameterValues

String arrayData[] = request.getParameterValues("arraydata[]");

To solve the second issue, you must iterate through the objects returned by Jquery's serializeArray method and construct an array of the values. The objects returned by serializeArray are in the form {name:val, value: val}. Use the following code to create and pass this array:

 var fields=new Array();
   var values = new Array();

   fields = $(":input").serializeArray();
   $.each(fields, function(index,element){
     values.push(element.value);
   });

  $.post("exampleservlet",{arraydata:values,mode:"Insert"},
  function(data) {
        alert("Data Loaded: " + data);
  });

Prior to this change the request you posted looked like this going across the wire:

enter image description here

After the change it uses a single dimension array, looking like:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I did some testing to see what was actually going over the wire. See updates, jsfiddle.net/NSheK/1
Thanks kevin for your great effect it's Works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.