Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

external .js file from servlet 1

Status
Not open for further replies.

birney29

Programmer
Joined
Oct 11, 2001
Messages
140
Location
GB
im trying to use an external JavaScript file from a servlet. the javascript code works fine in the servlet so theres nothing wrong with that but when i try to use it externally, it gives an error saying no object. as though it cant find the .js file. the directory ect is fine. any ideas?

thanks

kenny

code :

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

public class UIConfig extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.println(&quot;<html>&quot;);
out.println(&quot;<head>&quot;);
out.println(&quot;<title>&quot;);
out.println(&quot;UI config xml island test&quot;);
out.println(&quot;</title>&quot;);
out.println(&quot;<SCRIPT LANGUAGE=JavaScript SRC=UIConfig.js></script>&quot;);
// out.println(&quot;<script src=UIConfig.js></script>&quot;);
out.println(&quot;</head>&quot;);
out.println(&quot;<body onMouseMove=init() id=body>&quot;);
// out.println(&quot;<SCRIPT LANGUAGE='javascript'>&quot;);
// out.println(&quot; function init()&quot;);
// out.println(&quot; {&quot;);
// out.println(&quot; testXML.async = false;&quot;);
// out.println(&quot; testXML.load('../xml/ui.xml');&quot;);
// out.println(&quot;&quot;);
// out.println(&quot; formatUI(testXML);&quot;);
// out.println(&quot; }&quot;);
// out.println(&quot; function formatUI(testXML)&quot;);
// out.println(&quot; {&quot;);
// out.println(&quot; i = 0;&quot;);
// out.println(&quot; itemElement = testXML.documentElement.childNodes.item(i);&quot;);
// out.println(&quot; document.bgColor = itemElement.childNodes.item(0).childNodes.item(0).nodeValue;&quot;);
// out.println(&quot; document.fgColor = itemElement.childNodes.item(1).childNodes.item(0).nodeValue;&quot;);
// out.println(&quot; document.all.body.style.fontSize= itemElement.childNodes.item(2).childNodes.item(0).nodeValue;&quot;);
// out.println(&quot; } &quot;);
// out.println(&quot;</script>&quot;);
out.println(&quot;<p>Some Text that should be large<p>&quot;);
out.println(&quot;</body>&quot;);
out.println(&quot;<xml id=testXML></xml>&quot;);
out.println(&quot;</html>&quot;);
}
}
 
I don't believe that a .js file behaves like an include file like you want it to. Go ahead and try an include file there instead. Like this.
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UIConfig extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        res.setContentType(&quot;text/html&quot;);
        PrintWriter out = res.getWriter();
        
    out.println(&quot;<html>&quot;);
    out.println(&quot;<head>&quot;);
    out.println(&quot;<title>&quot;);
    out.println(&quot;UI config xml island test&quot;);
    out.println(&quot;</title>&quot;);
    out.println(&quot;<!-- #include file=&quot;UIConfig.js&quot; -->&quot;);
//    out.println(&quot;<script src=UIConfig.js></script>&quot;);
    out.println(&quot;</head>&quot;);
    out.println(&quot;<body onMouseMove=init() id=body>&quot;);
//    out.println(&quot;<SCRIPT LANGUAGE='javascript'>&quot;);
//        out.println(&quot;    function init()&quot;); 
//        out.println(&quot;    {&quot;);
//        out.println(&quot;        testXML.async = false;&quot;);
//        out.println(&quot;        testXML.load('../xml/ui.xml');&quot;);
//        out.println(&quot;&quot;);        
//        out.println(&quot;        formatUI(testXML);&quot;);
//        out.println(&quot;    }&quot;);
//        out.println(&quot;    function formatUI(testXML)&quot;);
//        out.println(&quot;    {&quot;);
//        out.println(&quot;        i = 0;&quot;);
//        out.println(&quot;        itemElement = testXML.documentElement.childNodes.item(i);&quot;);
//        out.println(&quot;        document.bgColor = itemElement.childNodes.item(0).childNodes.item(0).nodeValue;&quot;);
//        out.println(&quot;        document.fgColor = itemElement.childNodes.item(1).childNodes.item(0).nodeValue;&quot;);
//        out.println(&quot;        document.all.body.style.fontSize= itemElement.childNodes.item(2).childNodes.item(0).nodeValue;&quot;);
//        out.println(&quot;    }    &quot;);    
//        out.println(&quot;</script>&quot;);
    out.println(&quot;<p>Some Text that should be large<p>&quot;);
    out.println(&quot;</body>&quot;);
    out.println(&quot;<xml id=testXML></xml>&quot;);
    out.println(&quot;</html>&quot;);
  }
}

Now if you're already commenting out your JavaScript to hide from older browsers (like I do), then you'll need to close and re-open those comment tags like this.
Code:
out.println(&quot;//--><!-- #include file=&quot;UIConfig.js&quot; --><!--&quot;);

ToddWW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top