Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMlHttpRequest to a servlet

    Hi My client code is trying to communicate with a servlet and sending data in json format.

    Client code :

    var myJSONObject = {"name":"hello","address":"xyz"};

    var toServer = myJSONObject.toJSONString();
    var request=new XMLHttpRequest();
    request.open("POST", "http://localhost:8080/User/UserRegistration", true);
    request.send(toServer);
    return true;
    }
    catch(err)
    {
    alert(err.message);
    }
    }

    Servlet Code :

    public class UserRegistration extends HttpServlet {
    @Override
    public void doGet(
    HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    String output = request.getParameter("toServer");
    System.out.println(output);}}

    The data recieved at the servlet is always null..Can someone explain me the reason behind this?

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I have been through this problem and I have solved it this way:
    InputStream is = request.getInputStream();
    byte[] charr = new byte[is.available()];
    is.read(charr);
    String t = new String(charr,"UTF-8");

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks...but this doesn't seem to work for me..I am just getting a blank string on the servlet instead of a null..

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    In this case I think that the problem is in your function toJSONString().
    Try alerting the value of toServer to see what it is returning a value.

    if the value is empty or null then the problem is there.

  • #5
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have tried sending hard coded strings also....

    like

    var toServer="Hello";

    But again..i get a blank string at the server

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I want you to try something and that to make the call synchronous by doing the following:
    request.open("POST", "http://localhost:8080/User/UserRegistration", false);
    instead of
    request.open("POST", "http://localhost:8080/User/UserRegistration", true);

    By the way what browser you are using?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •