I am trying to understand several different ways to get dynamic content into a system I am building. These include:
1. <script tags to fetch text from a jsp page
2. <img to fetch images from a jsp page
3. AJAX to fetch text from a jsp page.
(In all cases, the jsp pages will eventually get the data from a MySql database, but right now I am using static code.)
I am having a problem understanding how to use the <script option.
I am starting with an html page with a javascript callback function and a <script with the callback passed as a parameter. The callback function is based on a sample that I found that works, but I don't have the code for the called jsp page.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test javascript WebServiceCalls...</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javaScript">
function ws_results1(obj)
{
alert("in ws_results1");
var myobj = obj.ResultSet.totalResultsAvailable;
alert("The result is "+myobj);
this.document.getElementById("showTest1").innerHTML = "The result is "+myobj;
}
</script>
</head>
<body >
<h2 > #1 <SCRIPT tag with immediate loading..... </h2>
WebServiceCalls #1: Use <script src="---.jsp?---&callback=---"> <br>
-----------------------------------------------<br>
Returned code gets inserted in calling page by callback function. <br>
NOTE: <br>
1. Page will delay loading until response is received. <br>
2. Target for callback function must preceed the %lt;script tag as it must be loaded first or it will not yet exist when the callback function executes.<br>
<br>
The following should be dynamically changed: <br>
<div id="showTest1">Please wait..</div>
The above line "Please wait.." should be replaced: <br>
<script type="text/javascript" src="TestWS1.jsp?callback=ws_results1"></script>
<br>
<br>
Rest of page loaded after return...
</body>
</html>
The jsp page is below. I am pretty sure that I am missing something here...
<%
// TestWS1.jsp: return a simple string.....
// (Other versions will return JSON structures or images)
// For page imports, wrap each line within a pair to avoid blanks and linefeeds...
%><%@ page import="java.lang.String" %><%
%><%@ page import="java.net.*" %><%
%><%@ page import="java.io.*" %><%
%><%@ page import="java.util.*" %><%
%><%@ page import="javax.servlet.*" %><%
System.out.println("TestWS1.jsp: begin");
// Use PrintStream to allow very long content made up of strings...
// thanks to: http://kickjava.com/1793.htm
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
PrintStream ps = new PrintStream ( baos ) ;
ps.println("\"TestWS1: This is the returned string as HTML <br> This can be any HTML code.<br>\"");
// now write the ByteArrayOutputStream to the outputstream
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 1);
response.setDateHeader("max-age", 0);
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.close();
System.out.println("TestWS1.jsp: done");
%>
When I run TestWS1.jsp, I get the expected entries in the system log and the expected output:
"TestWS1: This is the returned string as HTML <br> This can be any HTML code.<br>"
However, when I run the TestWS1Call.html, I get the entries in the system log but nothing fires the javascript function.
What am I missing?
Thank you