Jquery Post to Servlet

16,321

Solution 1

I think you want request.getParameter("orderId"). Attributes are only for server side use while processing the request. Parameters contain request data from the client side.

Solution 2

You should use getParameter method instead of getAttribute.

request.getParameter("orderId")

getParameter() will retrieve a value that the client has submitted. Where as you should use getAttribute() when you submit the request to another resource (server side).

Share:
16,321
danny.lesnik
Author by

danny.lesnik

Java Expert at Tikal Knowledge. Interesting in Spring/Hibernate/NoSQL/Spring MVC and other Java top edge technology. Twitter LinkedIn Facebook

Updated on June 11, 2022

Comments

  • danny.lesnik
    danny.lesnik almost 2 years

    I have the following code on client side:

          <script src="http://code.jquery.com/jquery-1.5.js"></script>
       <script>
        $(document).ready(function() {
       $("a").click(function() {
       //var orderId =  $("#orderId").val();
       $.post("test", { orderId : "John"},
       function(data) {
         alert("Data Loaded: " + data);
       });
       });
     });
        </script>
    

    Server side:

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            PrintWriter writer =  response.getWriter();
            try{
               String orderId = request.getAttribute("orderId").toString();
               writer.write(orderId);
               writer.close();
               }
           catch(Exception ex)
          {
          ex.getStackTrace();
          }
        }
    

    my

    request.getAttribute("orderId")
    

    is null and I'm getting null reference exeption. What am I doing wrong?