0

I am trying to create a table with sortable columns using a javascript library provided by this website. The web application is being created in Java and the table is created but not with the CSS style. I am 100% new to html and javascript and just started trying to learn things today.

I have placed the library in the same source folder as the JSP and also at the root of the project but both times the table is created in standard html format without the script formatting. How should I reference a Javascript library from a JSP in Java Web Application?

I have made some progress but I still cannot get it to work properly. This is my WelcomePage.jsp file:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%response.setHeader("Refresh", "5");%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>title</title>
        <script type="text/javascript" src="sorttable.js"></script>
    </head>
    <body>
        <p><a href=url target=\"_blank\">Start</a></p>
        <table class="sorttable">
            <tr>
                <td>text </td>
                <td>text </td>
                <td>text </td>
                <td>text </td>
                <td>text </td>
            </tr>
        </table>
    </body>
</html>

I have tried placing it into the META-INF, WEB-INF, and the root folders none of them seem to work correctly. I have also attempted to add it to the web.xml file but nothing seems to work.

file hierarchy

Some things I have already tried

Can not include javascript file from WEB-INF directory in JSP.

how to reference a java .class file from a JSP page?

Community
  • 1
  • 1
jeanqueq
  • 123
  • 2
  • 19
  • When you run the site in the browser, do you get any `404 not found errors` You'll want to use something like `Firebug` for `Firefox`, or the `Chrome Debugger` - `Press F12`, and go to the `Net` or `Network tab`, and see if all the files are loading. – Nick R Jun 21 '13 at 14:36
  • @NickR I do not see any errors in the logs or elsewhere in fact under the scripts tab in firebug it recognizes the sorttable.js file – jeanqueq Jun 21 '13 at 14:49
  • You could add an `console.log("js loaded")` or an alert `alert("test");` at the start of the `.js` file to ensure it actually runs on page load? – Nick R Jun 21 '13 at 14:55
  • @NickR It does run on load I believe, here are the files you can test yourself if you want. http://pastebin.com/bWBGWEkB http://kryogenix.org/code/browser/sorttable/sorttable.js – jeanqueq Jun 21 '13 at 15:00
  • 1
    http://jsfiddle.net/HZkz6/ - works fine here. – Nick R Jun 21 '13 at 15:02
  • @NickR wow ok I was expecting something different because it looks exactly the same as my table without the styling until you click on it. Thank you – jeanqueq Jun 21 '13 at 15:10

2 Answers2

0

Put it at the root of your application, or under a /scripts folder there. WEB-INF and META-INF are not accessible from outside.

If it is not accessible, then check the logs, try opening the js file manually in the browser, and check with Firebug (or similar tool) if there aren't javascript errors.

Then, if everything is ok, I suppose you need to invoke some function and pass the table id.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Alright, using Firebug I found out it is indeed accessible, but the table is still not formatting correctly. I do not see any errors. – jeanqueq Jun 21 '13 at 14:54
  • well, simply including the js probably won't do anything - you might have to invoke a function and pass the table id – Bozho Jun 21 '13 at 14:56
0

Since you are going to create web pages and you will probably need more js libraries, why don't you make a new folder inside the Web Pages one, solely for js?

brbtsl
  • 322
  • 1
  • 4
  • 14