SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Servlet returns successful after 1st Ajax request, fails every time after

    Hello

    I am testing an Ajax request and I am having a small issue with IE7 and FireFox.

    My goal is to run a small logging test. So from my JSP, I make an Ajax request which calls a servlet. The servlet uses a log4j logger and writes a message. I then want to return an indicator to the Ajax request telling the request the logging was completed.

    Right now I am just doing a proof of concept.

    Here is my problem. From my page, I have a link called Test. I click the link for the first time and everything works great. I have it set up so that a JavaScript alert displays after the servlet completes. So I click the link a 2nd time without refreshing the page and it seems like nothing happens (no JavaScript alert). So I go check the log, and I see the message was written out. So I go through this process again with debug in Eclipse and both times, the servlet is called. I put an alert in my JavaScript onreadystatechange that receives the response from the servlet but the response never gets here.

    Does someone know why the first time I run this, everything works perfect but the 2nd time, the servelt completes but the response does not make it back to my JavaScript? If I reload the page, the first time this Ajax process works again but each click after again does not return anything.

    Here is my servlet.

    Code:
    public class TestAjax extends HttpServlet
    {
    	private static Logger logger = Logger.getLogger(TestAjax.class);
        
    	public void doPost(HttpServletRequest req, HttpServletResponse res)
        {
    	String resCode = “”;
            Try{
    	   logger.info( “Logging Test Message” );
    	   resCode = “success”;
    	} catch (Exception e) {
    	   resCode = “fail”;
    	}
    
    	PrintWriter out = res.getWriter();
    	out.print( resCode );
    	out.flush();
            out.close();
        }
    }

    Any suggestions would be appreciated. Thanks in advance for your time.

  2. #2
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Several things...

    Do not manually flush or close the PrintWriter. Let the Web Container do those tasks. (I'm not super confident about calling flush on the writer. You might be allowed/able to do it, but I think in general its a bad idea.)

    I would change the behavior of your servlet just a little bit. I would have it forward to a JSP that is used for sending the confirmation back to the web browser. Look up "Model 2" for reasons why...

    Install LiveHttpHeaders add-on for firefox and see if you're getting the response back from the server. You might be, but their might be something wrong in the JavaScript. Not saying there is, just giving hints on how to debug.

    Best of Luck.
    baby steps... baby steps...

  3. #3
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for the response. That FireFox plug in really helped!!! You are right… it ended up being a JavaScript issue. I am not exactly sure what the issue is yet but at least I know where to look now. The reason I know it is JavaScript is that I have a reusable Ajax object and if I create a new instance of it each time, then this process works. I will have to find where my JavaScript reusability issue is at.

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,398
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try to use Firebug~ it's super awesome for this kind of AJAX debugging.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •