Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-09-2010, 07:24 PM   PM User | #1
lse
New Coder

 
Join Date: Oct 2009
Posts: 54
Thanks: 2
Thanked 0 Times in 0 Posts
lse is an unknown quantity at this point
I have a question about server side Ajax (Java) I think server error is

I have a question about server side Ajax (Java) I think server error is,

Quote:
Originally Posted by Leonidas Savvides
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
Code:
  <%-- 
    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
Code:
  /*
 * 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;
lse is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:48 AM.


Advertisement
Log in to turn off these ads.