NETBEANS: I get none ouput - can you detect?
I have a question about server side Ajax (Java) I think serverside error is,
Can you find the bug in <div> I do NOT get sample xml data[bookrss.xml]... RSS Client/Server?
I get
xhrequest.readyState == 4 && xhrequest.status == 200 ... 4 & 200,(js alert) but no data (local host run)
is this correct
1)var titles = xhrequest.responseXML.getElementsByTagName('title');
titles[0].firstChild.nodeValue+"</h1>";
2)xml file same folder as .java file in NETBEANS
???

start client side script

 <%-- 
    Document   : index
    Created on : Jul 22, 2010, 11:32:20 AM
    Author     : Shop
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        
        
  <script type="text/javascript">
// getxmlhttprequest.js
function getXMLHttpRequest()
{
 var xhrequest = null;
 if(window.XMLHttpRequest)
 {
 // If IE7, Mozilla, Safari, etc: Use native object
  try
  {
   xhrequest = new XMLHttpRequest();
   return xhrequest;
  }
  catch(exception)
  {
  // OK, just carry on looking
  }
 }
 else
 {

  // ...otherwise, use the ActiveX control for IE5.x and IE6
   var IEControls = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"];
   for(var i=0; i<IEControls.length; i++)
   {
    try
    {
     xhrequest = new ActiveXObject(IEControls[i]);
     return xhrequest;
    }
    catch(exception)
    {
     // OK, just carry on looking
    }
   }
  // if we got here we didn?t find and matches
  throw new Error("Cannot create an XMLHttpRequest");
 }
}

//processnewsfeed.js
function processRSSFeed()
{
      alert("alert processRSSFeed() "+ xhrequest.readyState + " " + xhrequest.status);
 if(xhrequest.readyState == 4 && xhrequest.status == 200)
 {
       alert("alert readyState/status success processRSSFeed()");
  var titles = xhrequest.responseXML.getElementsByTagName('title');
  var links = xhrequest.responseXML.getElementsByTagName('link');
  var descriptions = xhrequest.responseXML.getElementsByTagName('description');
  var firstItemTitle = "<h1>"+titles[0].firstChild.nodeValue+"></h1>";
  var firstItemLink = "<h3>"+links[0].firstChild.nodeValue+"</h3>";
  var firstItemDescription = "<h3>"+descriptions[0].firstChild.nodeValue+"</h3>";
  
  intro = firstItemTitle+firstItemLink+firstItemDescription;         
  //alert(links[0].firstChild.nodeValue);
for (var i=1; i<=3; i++)            
{
  var firstItemTitle2 = "<p>"+"<h2>"+titles[i].firstChild.nodeValue+"</h2>";
  var firstItemLink2 = "<h3>"+links[i].firstChild.nodeValue+"</h3>";
  var firstItemDescription2 = "<h3>"+descriptions[i].firstChild.nodeValue+"</h3>"+"</p>";   
  total += firstItemTitle2+firstItemLink2+firstItemDescription2+"";
  
}            
               
  document.getElementById("feed").innerHTML=intro+total;
 }
}      
      
      
function ajaxGetRSS()      
{      
  // no 'var', so this is a global variable!
  alert("alert ajaxGetRSS()");
    xhrequest = null;
    try
    {
     xhrequest = getXMLHttpRequest();
    }
    catch(error)
    {
     document.write("Cannot run Ajax code using this browser");
    }
    if(xhrequest != null)
    {
      xhrequest.onreadystatechange = processRSSFeed;
	  xhrequest.open("POST", "http://localhost:8084/ParsonsWebServicesAjax/rssfeed", true);
      xhrequest.send(null);
    }
}

</script>        
    </head>
    <body>
    <h2>Hello World!</h2>
    <br /><form action="" onsubmit="ajaxGetRSS();">
    <input type="submit" value="Press Appear Data" name="getData" />
    <br /></form>
    <a >click here</a>
    <p>&nbsp;</p>
    <div id="feed"></div>
    </body>
</html>

end client side
start Server Side Script

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Shop
 */
public class AjaxRSSServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        PrintWriter out = null;
        try {
        response.setContentType("text/xml");
        out = response.getWriter();

        File xmlfile = new File("bookrss.xml");
        
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(xmlfile));
        String s = br.readLine();
        while (s != null)
        {
            sb.append(s.trim());
            s=br.readLine();
            
        }
                System.out.println(sb);
                out.print(sb);
        } catch(IOException e) { 
            e.printStackTrace();
            //return null;
        }
        //return null;
    } 


}

end server side

start bookrss.xml

<rss version="0.91">
<channel>
<title>hT</title>
<link>hL</link>
<description>hD</description>
<item>
<title>hT1</title>
<link>hL1</link>
<description>hD1</description>
</item>
<item>
<title>hT2</title>
<link>hL2</link>
<description>hD2</description>
</item>
<item>
<title>hT3</title>
<link>hL3</link>
<description>hD3</description>
</item>
</channel>
</rss>

I see your method is "doPost()". I don't work with JSP, but IF in fact your server-side script is expecting a POST request, then the ajax you need to set the appropriate headers.
So, REPLACE xhrequest.send(null); with the following:

xhrequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhrequest.setRequestHeader('Content-Length',0);
xhrequest.send('');

line 65 alert("alert readyState/status success processRSSFeed()");
I make it run this alert but still get nothing in div, well?

tell me an alert of which test one item of xml data, this is correct?
alert(links[0].firstChild.nodeValue+"..."); // gives also nothing

<rss version="0.91"> <<this has any importance, version?

bookrss.xml
where must be in netbeans Project/subfolder? in webpages?

Please note my text Parsons java & xml cencage.co.uk, refers to that I posted not that you refer, has 80% of code ready, may be elsewhere the problem, javascript is ok ?

    PrintWriter out = null;
    try {
    response.setContentType("text/xml");
    out = response.getWriter();

    File xmlfile = new File("bookrss.xml");

    StringBuffer sb = new StringBuffer();
    BufferedReader br = new BufferedReader(new FileReader(xmlfile));
    String s = br.readLine();
    while (s != null)
    {
        sb.append(s.trim());
        s=br.readLine();

    }
            System.out.println(sb);
            out.print(sb);
    }

...javascript is ok ?

again, the only issue with the ORIGINAL ajax code you posted is that it is NOT sending a post request, BUT your servlet is expecting a POST. You need to also include what I gave you on post Sep 15th, 2010, 22:10 for your ajax to send the request as POST.

so javascript is ok, please note that i did this you refer but nothing changed, any debug tip?

For purposes of debugging, INSTEAD of reading from an XML file, have you tried simply sending a some "hardcoded" XML:

<root><test>testing</test></root>

just to verify that everything else is correct. If it works, then it is likely not able to locate the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.