my dog learned polymorphism*
The moose likes Other Java Products and Servers and the fly likes Servlet can't get the xmlhttp post request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "Servlet can Watch "Servlet can New topic
Author

Servlet can't get the xmlhttp post request

jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
why I xmlhttp post request can't send to java servlet..

//function sending request to servlet


location.java



--i tried to run my java servlet and it can save already the default headername and value to my repository...

[Devaka: Added code tags - please UseCodeTags when you post a code]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please check your private messages for an important administrative matter.


[My Blog]
All roads lead to JavaRanch
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
ok..i change it already..so sorry..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

req.open("POST","http://localhost:8080/GPS/location",true);

Shouldn't this be a GET method instead ?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
just post..because i'll just send the request info to my servlet. no response needed..

am i correct?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i tried to get..still don't work..any other idea...please
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Why are you overriding doGet in your servlet then ? Shouldn't you override doPost instead ?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i'll tried to use post and get...still don't work...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
"can't send" and "don't work" don't give us much to work with. What exactly happens if you run this code? Are there any messages in the servlet container logs? Are there any messages in the JavaScript Console? TellTheDetails


Ping & DNS - my free Android networking tools app
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
what i am trying to do is a widget that will send the latitude and latitude info to the database..i used xmlhttprequest to set the latitude and longitude as a header..

here is the flow.. first the gps info appears and there is a button in the widget that will send the gps info to the repository.i used servlet to get the parameter.so if i clicked the send button there is no response needed from the server just save the info to the database..the only thing that i can check f the info was saved is to manually view the data of the database..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
That's a start. Now:
Ulf Dittmer wrote:What exactly happens if you run this code? Are there any messages in the servlet container logs? Are there any messages in the JavaScript Console?

In addition to those: Is the HTTP request ever sent? (the Firefox extension LiveHttpHeaders can tell you). Is the servlet code ever called? (add logging to find out)
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
how can i find out that logs and http header? my application is already a widget..i can't find if the httprequest can send data to the servlet... when i try to run the servlet alone in my ide..it can save the default header paramter of my browser to the repository.. so meaning my servlet can accept and save data..the only problem is..i can't find out if my widget is sending any request and what request it is sending to my servlet because it is already a app..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
The JavaScript Console will tell you if there are any JavaScript errors on the client that prevent the request from being sent.

The Firefox extension LiveHttpHeaders will tell you whether the HTTP request is actually being sent.

The server log files will tell you whether there were any exceptions on the server that prevented a proper response from being sent. In addition, you can put logging statements in your servlet to track when it's being accessed, and other important information about the request/response.
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
when i run my widget here are the following error occurs:

in line 10 Info:setup:so:[object ServiceObject]


and another error in my error.js line 23 Info:

and last error when i click send button

location.js line 65 error:undefined value


here is my error.js


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
The line numbers you mention don't vibe with the line numbers in this code; which errors occur on which lines? Do the objects you're working with on those lines exist?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i fixed that problem already.but now i have a new problem

in my calback1 function..

in the line

var latitude = result.ReturnValue.Latitude;

error: undefined value...
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
var result was used in the other function..why is it in callback function it is found undefined..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
At that line, what values does "result" have"? What value does "result.ReturnValue" have? What value does "result.ReturnValue.Latitude" have? The quickest way to find out is probably the alert(...) function, or use a JavaScript debugger.
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
supposedly var result have a basiclocationinformation..where in callback function i just need to used that data---but why i can;t retrieve that data.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
"Supposedly" is not much help in debugging. What are the answers to the questions in my last post?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i never used debugger before..what i get is.value unknown in the current context
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
Are you familiar with the "alert(...)" function I mentioned earlier? It can be used to display values to the user from within JavaScript code.
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i add this ine to my code
var errCode=result.ErrorCode;
if(errCode){
alert("(003)GPS Error:"+errCode+""+result.ErrorMessage);
}
else{
alert(result.ReturnValue.Latitude,result.ReturnValue.Longtitude);
}

to find if there is error..but stillvariable result found undefined....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
Focusing on the result helps IF the request is ever sent ... so ... IS the request ever sent? Do you see it arrive on the server? Does the server send back the correct response?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
no the request never sent...I'm think the problem is in result variable because in callback1 function the error pointed to the line with variable result and if i add
return result; to my code no error appears but I can't find if there is value sent to the server.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
The name "result" sounds as if it would obtain a value after the response has been returned - but if no request is sent, that wouldn't happen, and thus result would never get a valid value. So I'd leave it aside and concentrate on figuring out why the request isn't sent.
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
any idea why the request can't send..I can't used javascript debugger because of this code: so = device.getServiceObject("Service.Location", "ILocation");==
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
If you can't use a debugger then you can use "alert" statements at strategic spots in the code to let you know which code path is being executed, and what values the important variables have.
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i used alert but it not work because the error undefined value occurs..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
We can't help if we don't know what the code is doing; see ItDoesntWorkIsUseless and TellTheDetails.
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i've explained the code before...my question is..what's wrong to my code why the callback function is not working..why the parameters sent to the callback supposedly is undefined





Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

function callback1(transId, eventCode, result)
....

callback1(result.ReturnValue.Latitude);

Why are you passing only one value ? result will be undefined if you don't pass transId and eventCode...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41830
    
  63
My last comment was about you saying that "alert" didn't work; but I assure you it works if used correctly. But we have no way of telling whether you are using correctly, because we don't know where you added it.

The code you posted now again is about "result" - I thought we had agreed that was irrelevant if the request wasn't being sent, yes?

To be very specific: Which lines of that code are being executed in which order? At which point do the variables no longer have the values they you think they should have? At which point is the flow of control different from what you think it should be? If there are JavaScript errors, which ones are they, and in which lines of code do they occur? What are the values of all relevant variables at that moment?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
how can i pass transId and event.. I never know how to use transId and event code..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I never know how to use transId and event code..

Are these two parameters used ? Do you need them ? Are you using them in the callback ? If not, remove them !
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
I tried to remove that before but someone told me that the correct syntax in callback is w/ transId and callevent..but i never know how to use and where i can put it to my code.
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
i remmove the trans id and eventcall STILL error occurs at line 29 of the last code i posted Error:Undefined Value
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

someone told me that the correct syntax in callback is w/ transId and callevent.

Well ask him where these parameters come from, and what they are used for.

If it's a callback, it may be a method that you don't have to call yourself. I guess that "so.ILocation.Trace(criteria, callback1)" will call the callback when the asynchronous request is over. Why are you calling it explicitly in line 10 ?
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
ah i just tried that code before..i forgot to remove...any idea what's wrong to my code...
jen lemque
Greenhorn

Joined: Jun 04, 2010
Posts: 28
now i know why my value is undefined because the send button that iam going to used to send the request to the servlet is address directly to cakkback function..so in directing to callback function other function that will get the value of the needed variable in the callback is not processing...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet can't get the xmlhttp post request