File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ajax and servlet problem

 
nik klaus
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to build a web application based on jsp ,ajax and servlets.The idea is to create a jsp that contains some options available for that user(previously he has to log in) and put a link on them.When the user clicks one of these links a xmlHttpRequest is send to the servlet informing it about this event ,the servlet performs some operations, and replys back.In the onreadystatechange I check this reply and if it is the expected one, I have to delete that link. The problem is that what ever I do ,the responseText comes back containing the html from the page that sent the request. What is it wrong ?






and the part in servlet where I respond to the request:
if (comanda.equals("register"))
{
int id=Integer.parseInt(request.getParameter("id"));
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().println("true");
}

previously I store some data in the session.
[ November 23, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that your Ajax request is correct and causing the servlet to execute as you think it is?
 
nik klaus
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ! everything woks fine until I try to use the responseText
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this point, I'm going to move this over to the JavaScript forum as the problem is unlikely on the server.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Time to post your client-side code.
 
nik klaus
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ November 23, 2008: Message edited by: nik klaus ]
 
nik klaus
Greenhorn
Posts: 8
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
steve claflin
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem with your code is that the returned message might not just be true -- it's more likely to be something like \n\ntrue\n

You should use a method like indexOf on the string to see if "true" is present somewhere in the returned text (of course, this is risky, since there is some slight change that the word "true" will appear in a generated error page with a stack trace, so it's usually best to come up with something a lot more complex that is unlikely to show up in the response accidentally)

If you did something like an alert(responseText) and saw that you are getting the html code for this page, then it seems likely that you are actually just re-requesting the same page.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic