Today made a display on the Web page stock Dynamic update.
But finally because of the IDE's different, and caused a lot of consequences, is: Very unhappy!!!
The thing is this:
At first, the code was written using MyEclipse. MyEclipse itself integrates a lot of tools, and many things can be configured well without having to set them manually. But for starters like me, it's not good.
Just start writing with MyEclipse, just write the main code is OK.
Later, to eclipse, there was always an error.
The code is as follows (only the main code is shown):
1,actionservlet part;
if ("/quoto". Equals (action)) {
Simulate the generation of stock information
List<stock> stocks = new arraylist<stock> ();
Random r = new Random ();
for (int i = 0; i < 8; i++) {
Stock S = new stock ();
S.setcode ("60023" + i);
S.setname ("Peak copper Electronics" + i);
S.setprice (R.nextint (500));
Stocks.add (s);
}
The Fromobject method can also be passed in an array
Jsonarray jsonobj = jsonarray.fromobject (stocks);
String jsonstr = jsonobj.tostring ();
Out.println (JSONSTR);
}
2,stock.jsp section:
<% @page pageencoding= "Utf-8" contenttype= "Text/html;charset=utf-8"%>
<style>
#d1 {
width:500px;
height:400px;
Background-color:black;
margin-left:400px;
margin-top:30px;
}
#d2 {
Color:yellow;
height:40px;
background-color:red;
}
Table {
Color:white;
font-size:24px;
Font-style:italic;
}
</style>
<script type= "Text/javascript" src= "Js/ajax.js" >
</script>
<script type= "Text/javascript" src= "Js/prototype-1.6.0.3.js" >
</script>
<script type= "Text/javascript" >
function Showstock () {
SetInterval (Quoto, 1000);
}
function Quoto () {
var xhr = getxhr ();
Xhr.open (' Get ', ' quoto.do ', true);
Xhr.onreadystatechange = function () {
if (xhr.readystate = = 4 && xhr.status = = 200) {
var txt = xhr.responsetext;
Converts a JSON string into a JS object
var arr = Txt.evaljson ();
var trs = ';
for (i = 0; i < arr.length; i++) {
var s = arr[i];
TRS + = ' <tr><td> ' + s.code + ' </td><td> ' + s.name
+ ' </td><td> ' + s.price + ' </td></tr> ';
}
$ (' tb1 '). InnerHTML = TRS;
}
};
Xhr.send (NULL);
}
</script>
<body style= "font-size:30px;" onload= "Showstock ();" >
<div id= "D1" >
<div id= "D2" > Stock real-time quotes </div>
<div id= "D3" >
<table width= "100%" >
<thead>
<tr>
<td> Code </td>
<td> name </td>
<td> Price </td>
</tr>
</thead>
<tbody id= "TB1" >
</tbody>
</table>
</div>
</div>
</body>
3,ajax.js part
function Getxhr () {
var xhr = null;
if (window. XMLHttpRequest) {
XHR = new XMLHttpRequest ();
} else {
XHR = new ActiveXObject (' microsoft.xmlhttp ');
}
return XHR;
}
This is the whole basic thing. However, when running on Tomcat, there is always an error indicating that ' quoto.do ' is not found. Several kinds of Anchor point tests and various checks were carried out, and the cause of the problem was not found. But this code runs on MyEclipse with no problem.
Later, there is really no patience, a line of comparison code, but when the CV Dafa fault it? The result is still a failure.
Rest for a while, look at the code, from the perspective of a bystander, do not put the operation of the problem at heart, and then this mentality, and finally found the reason:
The reason for the pit: in MyEclipse, the XML file is automatically configured without human involvement, but in Eclipse, the TM has to be handmade to match!!!!!!!!!!!!
One by one to configure various parameters.
Then, the parameter is less a *.do path, so, all the operation is nonsense!!!
At this time the mood will not have to say. The whole day was wasted here.
Summarize:
Various Ides, different configuration file generation and manual, various settings of different parameters.
PS: Not yet tested on NetBeans, but fortunately not tested on it, otherwise the pit is bigger.
It might make me a passer-by with eclipse.
Here's the lesson, write it down, and laugh at me.
~ Look, this idiot, still on the yards floor ~
JSON Learning Notes