...

View Full Version : I have a question about server side Ajax (Java) I think server error is



lse
08-09-2010, 08:24 PM
I have a question about server side Ajax (Java) I think server 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
client

<%--
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 = &quot;<p>"+"&lt;h2&gt;"+titles[i].firstChild.nodeValue+"&lt;/h2&gt;";
var firstItemLink2 = "&lt;h3&gt;"+links[i].firstChild.nodeValue+"&lt;/h3&gt;";
var firstItemDescription2 = "&lt;h3&gt;"+descriptions[i].firstChild.nodeValue+"&lt;/h3&gt;"+"</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);
}
}

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

server

/*
* 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 &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; 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;
}


}

bookrss.xml

&lt;rss version="0.91"&gt;
&lt;channel&gt;
&lt;title&gt;hT&lt;/title&gt;
&lt;link&gt;hL&lt;/link&gt;
&lt;description&gt;hD&lt;/description&gt;
&lt;item&gt;
&lt;title&gt;hT1&lt;/title&gt;
&lt;link&gt;hL1&lt;/link&gt;
&lt;description&gt;hD1&lt;/description&gt;
&lt;/item&gt;
&lt;item&gt;
&lt;title&gt;hT2&lt;/title&gt;
&lt;link&gt;hL2&lt;/link&gt;
&lt;description&gt;hD2&lt;/description&gt;
&lt;/item&gt;
&lt;item&gt;
&lt;title&gt;hT3&lt;/title&gt;
&lt;link&gt;hL3&lt;/link&gt;
&lt;description&gt;hD3&lt;/description&gt;
&lt;/item&gt;
&lt;/channel&gt;
&lt;/rss&gt;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum