Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet return value: JQuery Ajax request

 
Thomas Boss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i'm developing a Web project which includes a Login.

The login parameters are name and password.
I send this values with the help of the JQery framework to my servlet. In the Servlet i check the name and the password if they are valid.
My question is now what kind of return value do i need?
I have checked if the name and password reaches. This works but i don't get an answer back.

Here is my code:
Servlet:



Which type do i have to return?

Js:


HTML:


Thanking you in anticipation
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter what you return in doRequest(), it won't be sent to the client. What you need to do instead of returning the success/false string is writing it to the Writer returned by response.getWriter, in the js code you'll receive this value in request.responseText!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lorand Komaromi wrote:It doesn't matter what you return in doRequest(), it won't be sent to the client.

That's misleading. What's true is that the response must be written to the Writer returned by response.getWriter(). This can happen within doRequest().

in the js code you'll receive this value in request.responseText!

With jQuery, the result will automatically be passed to the success callback. No need to query the XHR instance.

To the OP: you can return whatever you like as the response, What makes the most sense for your page?
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
With jQuery, the result will automatically be passed to the success callback. No need to query the XHR instance.


result.responseText is what I meant, my bad!
 
Thomas Boss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks thanks for the good support!

That's misleading. What's true is that the response must be written to the Writer returned by response.getWriter(). This can happen within doRequest().



with jQuery, the result will automatically be passed to the success callback. No need to query the XHR instance.

To the OP: you can return whatever you like as the response, What makes the most sense for your page?


Now i'm writing in the case the correct password and name is insert a String object ("success") into the PrintWriter.
In the case the permission is denied i don't really need a respone. I only want to get some values in my Js function if the correct name and pw is insert.




result.responseText is what I meant, my bad!




in my navigate Js function i try to get the result.
Sadly i receive nothing :/

Hmm i think this should work. But i am a Js newbie and maybe i have forgotten something.

Thomas

 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out should be a local variable in doRequest! Your servlet instance is accessed by multiple threads, each of them overwriting out...
 
Thomas Boss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

out should be a local variable in doRequest! Your servlet instance is accessed by multiple threads, each of them overwriting out...




Ok i have changed it but i still didn't get any response in my Js function.

JS:


This function isn't called.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you didn't pay attention to my post. With jQuery you will be passed the result directly. Do not use any responseText nonsense.
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This probably isn't the cause of your problem, but you should check for null values (out and the request parameters). I'd put the rest of the code in doRequest in the try after the getWriter() call. If an exception is thrown there, there's no reason to continue execution.

Try to use the absolute URL of the servlet instead of ../Login!
 
Thomas Boss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try to use the absolute URL of the servlet instead of ../Login!


The input reaches my servlet. I have some System.out.println where the password and the name which i entered are displayed on the console.




This probably isn't the cause of your problem, but you should check for null values (out and the request parameters). I'd put the rest of the code in doRequest in the try after the getWriter() call. If an exception is thrown there, there's no reason to continue execution.


Also changed:


and removed the

responseText nonsense.



function login(){
var namenEingabe = document.loginForm.name.value;
var passwordEingabe = document.loginForm.password.value;
$.ajax({
type: "POST",
url: "../Login",
data: "name="+namenEingabe+"&pw="+passwordEingabe,
success: function(result){
if(result){
document.location = "menu.xml";
}else{
alert("Falscher Benutzername oder Passwort eingegeben!");
}
}
});
}


I didn't receive anything in the Js from my Servlet.



 
Thomas Boss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

I have found my mistake:


instead of



I think this was the result of pressing Crtl+Z.

Thanks for your posts.

 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, at least you've learned something new about servlet thread safety, the response's output stream, checking for nulls and "that responseText nonsense"!
 
Thomas Boss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, at least you've learned something new about servlet thread safety, the response's output stream, checking for nulls and "that responseText nonsense"!


You're right.

Thanks again for your support
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic