I have an issue in communication between my java server (based on sockets) and the content of a web page obtained by a java script.
The js asks to my server an xml and then the server replies using http, however the xml isn't shown. Using Firebug (mozilla plugin) I notice that the response http header is well received but there's no content.
Making the same request directly from a browser (I used mozilla and chrome) the xml is well returned and formatted.

here a bit of code...

java server code:

Java Code:
package Server;
		
		import java.io.DataOutputStream;
		import java.io.IOException;
		import java.io.OutputStream;
		import java.io.PrintStream;
		import java.io.PrintWriter;
		import java.net.ServerSocket;
		import java.net.Socket;
		
		public class ClasseMain {
		
			/**
			 * @param args
			 * @throws IOException 
			 */
			public static void main(String[] args) throws IOException {
				int i =0;
				while(true){
					try {
						i++;
						normalSocket();
					} catch (Exception e) {
						System.out.println("errore iterazione n: "+i);
					}
		
				}
			}
		
		
			static void normalSocket()
			{
				try{
					ServerSocket s= new ServerSocket(8080);
					Socket c = s.accept();
		
					System.out.println("\n\n\n\nConnessione stabilita");
					PrintWriter o = new PrintWriter(c.getOutputStream(),true);
		
					o.println("HTTP/1.1 200 OK\n"+
							//					"Server: nginx/1.0.4\n"+
							//					"Date: Fri, 10 Jun 2011 15:49:08 GMT\n"+
							"Content-Type: text/xml; charset=utf-8\n\n" +
							//					"Transfer-Encoding: chunked\n" +
							//					"Connection: keep-alive\n" +
							//					"X-Powered-By: PHP/5.3.5-1ubuntu7.2\n" +
							//					"P3P: CP="+'"'+"NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"+'"'+"\n\n\n" +
							//					"225\n" +
							"<?xml version="+'"'+"1.0"+'"'+" encoding="+'"'+"UTF-8"+'"'+" ?>\n" +
							"<refresh>\n" +
							"<item>ciao ciao</item>\n"+
					"</refresh>");
		
		
					System.out.println("HTTP/1.1 200 OK\n"+
							//					"Server: nginx/1.0.4\n"+
							//					"Date: Fri, 10 Jun 2011 15:49:08 GMT\n"+
							"Content-Type: text/xml; charset=utf-8\n\n" +
							//					"Transfer-Encoding: chunked\n" +
							//					"Connection: keep-alive\n" +
							//					"X-Powered-By: PHP/5.3.5-1ubuntu7.2\n" +
							//					"P3P: CP="+'"'+"NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"+'"'+"\n\n\n" +
							//					"225\n" +
							"<?xml version="+'"'+"1.0"+'"'+" encoding="+'"'+"UTF-8"+'"'+" ?>\n" +
							"<refresh>" +
							"<item>ciao ciao</item>"+
					"</refresh>");
		
					c.shutdownOutput();
					s.close();
				}catch (Exception e)
				{
					System.err.println("errore");
					e.printStackTrace();
				}
		
			}
		
		}

I tried to use different types of stream (as you can see in the imports) to outputting the response, with no success.

Client side development isn't my responsibility, however I post the js and html code for completeness:

html:

XML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml"><head>
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">	<title>
				CakePHP: the rapid development php framework:		Control	</title>
			<script type="text/javascript" src="control_files/jquery.js"></script><link href="http://192.168.1.3/favicon.ico" type="image/x-icon" rel="icon"><link href="http://192.168.1.3/favicon.ico" type="image/x-icon" rel="shortcut icon"><link rel="stylesheet" type="text/css" href="control_files/cake.css"><link rel="stylesheet" type="text/css" href="control_files/menu.css"></head>
		<body>
			<div id="container">
				<div id="header">
							<ul id="main-menu" class="menu">
			<li class="first-item"><a title="Home" href="http://192.168.1.3/eng/admin">Home</a></li>
			<li><a title="User" href="http://192.168.1.3/eng/users">User</a></li>
			<li><a title="Sponsor" href="http://192.168.1.3/eng/users/sponsor">Sponsor</a></li>
			<li><a title="Style" href="http://192.168.1.3/eng/stylecsses">Style</a></li>
			<li><a title="Badword" href="http://192.168.1.3/eng/badwords">Badword</a></li>
			<li><a title="Propriety" href="http://192.168.1.3/eng/proprieties">Propriety</a></li>
			<li><a title="Identify" href="http://192.168.1.3/eng/identifies">Identify</a></li>
			<li class="active"><a title="Control" href="http://192.168.1.3/eng/control">Control</a></li>
			<li><a title="Activity" href="http://192.168.1.3/eng/actionproprieties">Activity</a></li>
			<li><a title="Logout" href="http://192.168.1.3/eng/users/logout">Logout</a></li>
		</ul>
							</div>
				<div id="content">
											
					<link rel="stylesheet" type="text/css" href="control_files/countdown.css"><script type="text/javascript">
				/*
				var URL_REFRESH = "http://192.168.1.196:8080/admin.xml";
				var URL_PROCESS = "http://192.168.1.3/eng/actioninboxes/setProcess";
				var URL_SENDSMS = "http://192.168.1.3/eng/actioninboxes/saveMessageFromPC";
				var URL_DISCO = "http://192.168.1.3/eng/actionproprieties/getActivitiesOn";
				*/
				
				var URL_REFRESH = "http://127.0.0.1:8080/admin.xml";
				var URL_PROCESS = "http://192.168.1.3/eng/actioninboxes/setProcess";
				var URL_SENDSMS = "http://192.168.1.3/eng/actioninboxes/saveMessageFromPC";
				var URL_DISCO = "http://192.168.1.3/eng/actionproprieties/getActivitiesOn";
				
						var REFRESH = 2000;
				var PROCESS = new Array();
				PROCESS[0] = new Array(); PROCESS[0]['name'] = 'da_approvare'; PROCESS[0]['values'] = ''; PROCESS[1] = new Array(); PROCESS[1]['name'] = 'approvato'; PROCESS[1]['values'] = '1'; PROCESS[2] = new Array(); PROCESS[2]['name'] = 'onair'; PROCESS[2]['values'] = '2'; PROCESS[3] = new Array(); PROCESS[3]['name'] = 'visto'; PROCESS[3]['values'] = '3'; PROCESS[4] = new Array(); PROCESS[4]['name'] = 'recheck'; PROCESS[4]['values'] = '4'; PROCESS[5] = new Array(); PROCESS[5]['name'] = 'eliminato'; PROCESS[5]['values'] = '9'; 
		</script>
				<script type="text/javascript" src="control_files/date.js"></script>		<script type="text/javascript" src="control_files/countdown.js"></script>				<script type="text/javascript" src="control_files/countdown-it.js"></script>		<script type="text/javascript" src="control_files/timeout.js"></script>		<script type="text/javascript" src="control_files/control.js"></script>                <script type="text/javascript" src="control_files/sendsms.js"></script>		
		
		
		<div class="controlphones index">
			<h2>
			Control	</h2>
			<div id="return"></div>
			<table id="control" cellpadding="0" cellspacing="0">
				<tbody><tr>
					<th>SMS</th>
					<th>SENDER</th>
					<th>LOCATION</th>
					<th>END</th>
					<th class="actions">Actions</th>
				</tr>
			</tbody></table>
		</div>
		
		<div class="actions">
		
			<h3>Sms AJAX</h3>
		
		<form id="ActioninboxIndexForm" method="post" action="/eng/actioninboxes" accept-charset="utf-8"><div style="display:none;"><input name="_method" value="POST" type="hidden"></div>	<fieldset>
			<div class="input select"><label for="ActioninboxUserId">User</label><select name="data[Actioninbox][user_id]" id="ActioninboxUserId"><option selected="selected" id="DiscoOnline" value="22">ITdisc1</option></select></div><div class="input textarea"><label for="ActioninboxSmssent">Smssent</label><textarea name="data[Actioninbox][smssent]" cols="30" rows="6" id="ActioninboxSmssent"></textarea></div><input name="data[Actioninbox][idnation]" id="ActioninboxIdnation" type="hidden">	</fieldset>
		<div class="submit"><input value="Submit" id="submit1698940554" onclick="return false;" type="submit"></div><script type="text/javascript">
		//<![CDATA[
		$("#submit1698940554").bind('click', function(){ $.ajax({async:true, type:'post', url:'/eng/Actioninboxes/saveMessageFromPC', data:$(this).parents('form:first').serialize()}); $("#ActioninboxSmssent").val(""); })
		//]]>
		</script>
		</form></div>
		
				</div>
				<div id="footer">
					<a href="http://www.cakephp.org/" target="_blank"><img src="control_files/cake.gif" alt="CakePHP: the rapid development php framework" border="0"></a>		</div>
			</div>
					
		</body></html>
js:

Java Code:
$(window).load(refresh());
			
		function refresh() {
			getXML();
			
			//setInterval(function(){getXML();},REFRESH);
		}
		
		function getXML(){
			$.ajax({
				type: "POST",
				url: URL_REFRESH,
				dataType: "xml",
				success: parseXml
			});
			
			$.timeout(refresh, REFRESH);
		}
		
		
		var i = 0;
		var DIFFERENCE =  new Date();
		
		function parseXml(xml){
			//alert($(xml).text());
		
				//impostazione data server
			syncroDate($(xml).find('servertime').text());
		
		
			//Messaggio
			removeMessages();
			i=0;
			$(xml).find('messages').find('message').each(function(){
				//alert($(this));
		
				setMessage($(this));
			});
		
			//Sponsor
			/*$(xml).find('sponsor').each(function(){
		
			});*/
		
			//settings
			$(xml).find('settings').each(function(){
				REFRESH = $(this).find('timer').text();
			});
		}
		
		
		
		function setMessage(msg){
			//alert(msg);
		
			i++;
		
			var message = "<tr id='controlmsg' class='"+i+"' > <td>"+ msg.find('smsadministrator').text()+
			"</td><td>"+msg.find('sender').text()+
			"</td><td>"+msg.find('name').text()+
			"</td><td><div id='end"+i+"'></div></td><td><div id='actions"+i+"'></div></td></tr>";
		
			if(msg.find('id').text() != ''){
				$("#control").append("<tbody>").append(message);
				endMessage(msg.find('end').text());
				//var mess = msg.find('actioninbox');
				actionMessage(msg.find('process').text(), msg.find('actioninbox').find('id').text());//TODO passare l'id del messaggio
			}
		}
		
		function removeMessages(){
			$('table').find('#controlmsg').each(
					function(){
						$(this).remove();
					});
		}
		
		function endMessage(end){
			if(end != ''){
				var date = new Date(); 
				date = Date.parseExact(end, "yyyy-M-d H:mm:ss"); //date = Date.parseExact("2011-04-14 13:59:55", "yyyy-M-d H:mm:ss"); 
				date = date.getTime() + DIFFERENCE;
						date = new Date(date);
		//alert(date);
						$('#end'+i).countdown({until: date, format:'MS'});
			}
		}
		
		function actionMessage(state,id){
			state = (state*1)+1;
			var actions = '';
			for(var k=state; k<PROCESS.length;k++){
				if(PROCESS[k]['name'] != 'onair' && PROCESS[k]['name'] != 'visto' && PROCESS[k]['name'] != 'recheck'){
					actions += '<a href="javascript:actionSubmit('+id+', '+PROCESS[k]['values']+')">'+PROCESS[k]['name']+'</a> ';
				}
			}
			$('#actions'+i).append(actions);
		}
		
		
		function actionSubmit(id, process){
			var href = URL_PROCESS+'/'+id+'/'+process;
			$.get(href,{},function(response){ 
				   $('#return').html(response).delay(1800).hide('slow').show();//x fdarlo scomparire dopo 800 millisecond e poi lasciarlo visibile.
				});
		}
		
		
		function syncroDate(dateserver){
			var server = new Date();
			server = Date.parseExact(dateserver, "yyyy-M-d H:mm:ss");
		
			var locale = new Date();
		//alert(locale);
			DIFFERENCE = (locale.getTime() - server.getTime());
		}

finally a screenshot of Firebug error console

Errore interpretazione XML: nessun elemento trovato Indirizzo: moz-nullprincipal:{9e2adab2-b678-424a-96eb-9c6ad78bdd19} Linea numero 1, colonna 1:

^
it means: error xml parsing: no element found .... line 1 col 1

ImageShack® - Online Photo and Video Hosting

ImageShack® - Online Photo and Video Hosting


Thanks to everyone will help me (i'm going completely mad) on how to resolve the problem or suggest a solution