I'm trying to get a servlet to use a routine in an 
exteral javascript .js file.  So far, I haven't been 
able to figure out how to 'knit' everything together so 
the routine is resolved.

Below are two **very** simple java servlet programs, 
JsStep1.java and JsStep2.java.  There is also a file, 
external.js with a simple routine, externalhello() that 
puts up an alert box to indicate it was reached.

I'm using TomCat 3.2.1 with Win-2000 SP1 and Ie5.5 Sp1.  

The .java and .class files are in 
h:\Jakarta\webapps\ari\WEB-INF\classes

JsStep1 invokes an internal javascript routine, 
internalhello().  This works fine, and the alert box 
shows up.

JsStep2 attempts to invoke an external javascript 
routine, externalhello(), and crashes.  TomCat reports 
an error, 
Ctx( /ari ): 404 R( /ari + /servlet/external.js + null) 
null

I also get an runtime error message:
Line: 11
Error: Object expected

These are the same symptons/messages I got before I had 
put external.js anywhere, which makes me think that 
Tomcat can't find the file.

When I save the source as JsStep2.html, it works fine in 
a browser; in that it can reach external.js and put up 
the alert box.

I'm stumped ... what am I doing wrong?  My guess is that 
I don't have the external.js file in the correct 
directory, but I've tried invoking JsStep2 with 
external.js in 14 different directories ... any 
directory that seemed to make any sense.  I've tried 
different capitalizations, with and 
without "javascript:" prefix.  Is there something I 
should put in the web.xml file?

Below are the three files.

********* JsStep1

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class JsStep1 extends HttpServlet {
   public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
                     throws IOException, 
ServletException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      out.println("<html>");
      out.println("<head>");
      out.println("<title>JsStep1-d</title>");
      out.println("<script language=\"javascript\">");
      out.println("function internalhello() {");
      out.println("   alert(\"Reached 
internalhello\");");
      out.println("}");
      out.println("</script>");
      out.println("</head>");
      out.println("<body bgcolor=blue>");
      out.println("<h1>JsStep1-d</h1>");
      out.println("Test_1: <input name=\"test_1\" 
type=\"text\" ");
      out.println("  onchange=\"javascript:internalhello
();\">");
      out.println("Test_2: <input name=\"test_2\" 
type=\"text\" ");
      out.println("  onchange=\"internalhello();\">");
      out.println("</body>");
      out.println("</html>");
   }
}

******** JsStep2 *************

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class JsStep2 extends HttpServlet {
   public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
                     throws IOException, 
ServletException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      out.println("<html>");
      out.println("<head>");
      out.println("<title>JsStep2-d</title>");
      out.println("<script language=\"javascript\" 
src=\"external.js\">");
      out.println("</script>");
      out.println("</head>");
      out.println("<body bgcolor=blue>");
      out.println("<h1>JsStep2-d</h1>");
      out.println("Test_1: <input name=\"test_1\" 
type=\"text\" ");
      out.println("  onchange=\"javascript:externalhello
();\">");
      out.println("Test_2: <input name=\"test_2\" 
type=\"text\" ");
      out.println("  onchange=\"externalhello();\">");
      out.println("</body>");
      out.println("</html>");
   }
}



******** external.js  ********

function externalhello() {
   alert ("Reached externalhello");
}

Reply via email to