views:

43

answers:

2

From a servlet, I'm forwarding the request to a JSP page which renders a FusionChart. But I've a problem in loading the chart. The JSP file is not detecting the JavaScript file. The folder structure is:

axis
 |
 WebContent
      |
     WEB-INF
        |
       classes
         |_ com 
         |_FusionCharts.js
         |_MyChartJsp.jsp
         |_Line.swf

And the JSP code:

<html>
<head>
<script language="text/javascript" src="/WEB-INF/classes/FusionCharts.js"></script>
</head>
<body bgcolor="#ffffff">
<div id="chartdiv" align="left">The chart will appear within
this DIV. This text will be replaced by the chart.</div>
<script type="text/javascript">

var foo = //value fetched from DAO
var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
            "myChartId", "1000", "500");

    myChart
            .setDataXML("<graph caption='aCaption' xAxisName='xAxis' yAxisName='yAxis' showNames='1' decimalPrecision='0' formatNumberScale='0'>"+foo+"</graph>");
    myChart.render("chartdiv");
</script>

</body>
</html>

The Servlet code to forward the request:

final RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/classes/MyChartJsp.jsp");
requestDispatcher.forward(request, response);

The request is getting forwarded to the JSP. But the chart is not getting displayed because it is unable to figure out what FusionCharts is in the line

var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
                "myChartId", "1000", "500");

I tried

src="/FusionCharts.js"

src="FusionCharts.js"

but no luck.

Has it something to do with the request being forwarded??

+6  A: 

You cannot have .js (or .swf, .jpg, etc.) files in WEB-INF - they are not publically accessible.
Move it to /js/

There is no reason to hide static resources (like scripts and css) in WEB-INF. If you insist on that, you should make a servlet that, given the name of the js/css, would read it from its location and will serve it as a response. This is what the default servlet does when you access static resources.

The flow of the page loading is as follows: the browser sends a request to the servlet; the servlet forwards internally to the JSP, and the JSP is rendered as a response; then the browser parses the <script> tag and fires another request to the script. If the script is not accessible via URL, it's not loaded.

Then, to make the script url fixed to the servlet context root, use

src="<c:url value="/js/script.js" />"

This will work regardless of what is the current url

Bozho
It should also be noted that the Line.swf file will not be accessible either. Like the javascript file, it should be moved out from under the WEB-INF folder.
jt
I've moved .js file out of WEB-INF. But still no luck. It did worked when I moved the JSP file too out of WEB-INF. All the three(.js, .jsp, .swf) files I placed "WebContent/FusionCharts/" and it worked. Is it OK if I place JSP file outside of WEB-INF?
HanuAthena
@HanuAthena see update
Bozho
A: 

Not the cause of your problem, but also note that your <script> element is incorrect. It should be <script type="text/javascript"....

(I tried to post this as a comment, but for some reason it wouldn't let me.)

John Topley
I've corrected it.
HanuAthena
In HTML5, the `type` attribtue **defaults** to `text/javascript`. So if you're using a HTML5 doctype, you can safely omit it (even then, in HTML4 the w3c validator would be the only one who jerks; it would have *worked* in all browsers from IE6 and on).
BalusC