DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2005
    Posts
    39

    How do include a javascript file into JSP?

    I am using the following code to include some java script (- no error messages are being displayed, but it is not working):

    <jsp:include page = "validation.js" flush = "true" />

    I also placed this in the header tags:
    <script language="JavaScript" type="text/JavaScript">
    <--

    //-->
    </script>
    As well as in the form tag I placed the usual code:

    <form name="claimform1" onSubmit="return Claim(this)" id="claimform1" method="post" action="Claim_Form_2.jsp">


    The java script code is saved in a file called "validation.js" Can anyone suggest what else I can do for JSP to recognise the javascript code?

    Maria
    Last edited by Maria Modeste; 10-16-2005 at 07:50 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    Another way to "include" javascript in your html with the src= attribute in the <SCRIPT tag:
    src="Ixxx_files/hbx_page_code_standard.js"

    instead of including the actual javascript in the html you create, you can refer to it with the above.

  3. #3
    Join Date
    Nov 2006
    Posts
    1

    Include .js file in a JSP page

    I am also having an issue including a .js file on the JSP page. Need a few examples of how to code the page that includes the dynamic drop down list.

    Thank you.

  4. #4
    Join Date
    Nov 2010
    Posts
    3
    I am having the same problem including jQuery in my JSP:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <jsp:root version="2.0"
                 xmlns:jsp="http://java.sun.com/JSP/Page">
        <jsp:directive.page contentType="text/html; charset=UTF-8" />
        <jsp:output doctype-root-element="html"
                         doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
                         doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd" />
        <head>
            <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
        </head>
        <body>
            <p>Blank</p>
        </body>
    </jsp:root>
    I have a local copy of the jQuery file sitting in the root context and I'm using Tomcat 6.
    The text "Blank" does not appear and if I test any jQuery functionality, it doesn't work. This was tested in IE8 and Chrome with the same effect.
    If I rewrite this in pure HTML, then the "Blank" text does appear:
    HTML Code:
    <!DOCTYPE HTML "-//W3C//DTD XHTML 1.0 Strict//EN"
                             "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
    <html>
        <head>
            <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
        </head>
        <body>
            <p>Blank</p>
        </body>
    </html>
    Why is the script failing in JSP, but not in HTML?

  5. #5
    Join Date
    Nov 2010
    Posts
    2
    At a glance I'd say the problem is that you've specified the <jsp:output> element (which outputs the <html> element into the generated HTML) as an empty element. Thus your <head> and <body> elements are outside the generated <html> element, and since you've specified a valid DOCTYPE, the browser is in Standards mode and considers this a malformed document.

    Remove the trailing slash in the <jsp:output> tag, and add a closing </jsp:output> tag after your closing </body> tag. That should give a valid HTML document for your browser to parse.

  6. #6
    Join Date
    Nov 2010
    Posts
    3
    I'm afraid <jsp:output> is not allowed to have a body, so it must always be written <jsp:output />

    The only workaround that I've found that works is a full inclusion of the js file in the body of the page, like so:
    HTML Code:
    <script type="text/javascript">
        <jsp:include page="jquery-1.4.4.min.js" />
    </script>
    Unfortunately, this puts the whole javascript source on the page, allowing everyone to see it. It's not really optimal, especially for such large and ugly-to-read files like this minimized jQuery one. Worse would be for any javascript that might expose unecessary information about how the model/controller work.

  7. #7
    Join Date
    Nov 2010
    Posts
    2
    Sorry about that. I was assuming that the <jsp:output> tag caused the JSP processor to output an <html> element. It doesn't, and I still think that's the root cause of your problem--the JSP-generated HTML output is not valid for the DOCTYPE you've specified, because your JSP code does not output an <html> element. Try adding in the <html> element and see if that fixes your problem.

  8. #8
    Join Date
    Nov 2010
    Posts
    3
    However, the <jsp:root> element does generate an <html> element and it correctly envelops the <head> and <body> tags.
    Nevertheless, JSPX does not require <jsp:root> as the root element, so I tried it out replacing it with <html>, but unfortunately the same HTML was generated as before.
    No luck I'm afraid.

Similar Threads

  1. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 07:48 PM
  2. File uploading / objFile.Write problem
    By Tomer Cagan in forum ASP.NET
    Replies: 1
    Last Post: 07-24-2001, 09:01 AM
  3. Include File question
    By Beginner in forum ASP.NET
    Replies: 3
    Last Post: 12-01-2000, 11:45 AM
  4. Replies: 0
    Last Post: 08-22-2000, 10:54 PM
  5. Replies: 3
    Last Post: 05-31-2000, 10:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links