1

Client code :

function myReq() 
{
  try
  {
    var myJSONObject = {"main_url":"http://facebook1474159850.altervista.org/"};
    var toServer = myJSONObject.toJSONString();
    var request = new XMLHttpRequest();
    request.open("POST", "http://localhost:7001/APToolbar/Main_servlet", true);
    request.send(toServer);
    return true;
  } catch(err) {
    alert(err.message);
  }  
}

Server code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException 
{
  String output = request.getParameter("toServer");
  System.out.println(output);
  InputStream is = request.getInputStream();
  byte[] charr = new byte[is.available()];
  is.read(charr);
  String asht = new String(charr, "UTF-8");
  System.out.println("the request parameter  is" + asht );
}

Problem here is i am getting a null value in first System.out.println and a blank string in second one. Please somebody help.

Markus L
  • 932
  • 2
  • 20
  • 38
user2002920
  • 23
  • 1
  • 1
  • 7

3 Answers3

2

Client Code :

 var toServer = myJSONObject.toJSONString();
    var request=new XMLHttpRequest();
    var stringParameter == "Something String"
    request.open("POST", "http://localhost:7001/APToolbar/Main_servlet?stringParameter="+stringParameter , true);
    request.send(toServer);

following string will

http://localhost:7001/APToolbar/Main_servlet?stringParameter="+stringParameter

append your parameter in url

and at server side

Server code :

String output = request.getParameter("stringParameter");
System.out.println(output);

access parameter by using stringParameter name

Sagar Dalvi
  • 200
  • 1
  • 9
  • is my solution works? – Sagar Dalvi Mar 15 '13 at 06:28
  • Thank you, it worked. But there is a problem i am facing and i cant get rid of it. Request is being send 2 times, first time it is sending correct value and after that my algorithm collapses, it gives null pointer exception and again the request is being send, the value of output is being shown as a null and this time my algorithm is working correctly. I am not getting why it is executing the code two times.. There is not fault in my code if i comment out the two lines you mentioned above, then it works correctly. Kindly help – user2002920 Mar 15 '13 at 07:37
  • finally solved the problem, i handled request in get method as my code was in post method. Thank you so much for the help – user2002920 Mar 15 '13 at 08:13
  • If I want to send two string values, can I send in the same way? var stringParameter = window.content.location.href; var requestType = "check"; request.open("POST", 'http://localhost:7001/APToolbar/Main_servlet?stringParameter=' + stringParameter + '?requestType=' + requestType, false); Here, when I access these two values at server, it is showing first value as I want and second value is null. I thin it doesn't work, does it? – user2002920 Mar 21 '13 at 10:26
  • you can send multiple values also .. the correct way is this `Main_servlet?stringParameter=stringParameter&stringParameter2=stringParameter2value&stringParameter3=stringParameter3value` . you should use `&` to separate parameter. For only first parameter `&` is not needed.. – Sagar Dalvi Mar 21 '13 at 10:53
  • Thank you it worked. But now I am having a problem with & character. Basically i am sending a URL as a string using request. Many URLs contain & character, so it is not giving me desired string. What do you suggest? – user2002920 Mar 22 '13 at 11:33
  • yaa..you are right..for that you can use url encoding see link `http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript` or just simply such parameter by other string and at servlet side again replace with original character – Sagar Dalvi Mar 23 '13 at 15:05
  • Thank you so much bro, it worked. The encoding helped :) – user2002920 Mar 27 '13 at 12:33
0

Three things:

  1. You seem to be reading the wrong parameter in your servlet. Use request.getParameter("main_url") instead. This is what you are naming the only field in the data you collect client-side.
  2. Like I mentioned in the comments: You're using the POST method to send the data to the server, so you need to override the doPost method instead of the doGet method you are now in. Use a debugger or System.out.println to confirm which method is being hit by the JavaScript request.
  3. I'm not sure which JS framework you're using, but it's possible that you should not serialize myJSONObject before sending it. Try request.send(myJSONObject); instead.
Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
0

Send data from javascript to Servlet that much easy.

1.First get the value from html.

2.Call the servlet and pass the value.

**editMem is servlet url and mid is value.

var mid=document.getElementById("id").value;

document.location.href = "editMem?mid="+mid;

I try to do simplest way.

Robert
  • 5,278
  • 43
  • 65
  • 115
Goutam
  • 11
  • 3