Skip to main content

jmaki.doAjax Login problem with the use of a servlet

3 replies [Last post]
Joined: 2008-04-13


i have the following problem. I use jmaki.doAjax to post the username and password from an simple HTML form using a Yahoo Button. the doAjax function posts to a servlet that uses an EntityManager and a NamedQuery to validate the user, then returns a string of either "failed" if the use is not found or "success" if found, by using the out.println() function...the problem is when i try to test the _req.responseText using a simple if statement, it always returns false even though the strings match....the code should clarify better!!


jmaki.doAjax({method : "POST", url : "LoginServlet?benutzername=" + Benutzername + "&passwort=" + Passwort + "",
callback: function (_req) {
var response = _req.responseText;
if (response == "failed") alert("user doesn't exist");
else alert("user exists");


******servlet code*******

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
//use the entity bean to validate the user
EntityManagerFactory emf = Persistence.createEntityManagerFactory("kewPU");
EntityManager em = emf.createEntityManager();
Query Login = em.createNamedQuery("Anwender.Login");
Login.setParameter("user", request.getParameter("benutzername"));
Login.setParameter("pass", request.getParameter("passwort"));
List anwender = Login.getResultList();

if (anwender.isEmpty())

else out.println("success");


NOW..the problem is that when a user is not found, the servlet returns a string with a value of "failed", however the if statement that checks _req.responseText == "failed" returns false....i have spent more than 5 hours straight on this...i have tried to create a JSON object and test against the members of that object...i have tried everything i can think of....please help

thank you in advance

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-07-31

Are you using IE as your browser?

If you are the problem may be that your servlet is caching the response.

Can you verify that the req.responseText really is false? I suspect you might be getting a cached value. If this is the case see for more on this

More specifically on your serlvet code try adding the following:

response.setHeader("Cache-Control", "no-cache");

This will keep IE from returning the same message every time.

Let us know if this does not work.


Joined: 2008-04-13


i followed the directions of the article at the link you gave me, however i receive an error from the javascript code, saying that the object doesn't support this method or property....which i assume is this following line of code:

var message = _req.responseXML.getElementsByTagName("message")[0];

so i decided to follow your advice about getting a cached value and added this line to my servlet code:

response.setHeader("Cache-Control", "no-cache");

and now it works just my next question is will that line of code provide support to more than the IE browser, or is there additional code for that

thank you

Joined: 2003-07-31

The cache control needs to be set on the Servlet side on the servlet request. It should support all browsers.