0

On my JSP project I've got a .jsp file which contains the following script:

<script type="text/javascript">
    $(window).load(function() {
        ...
        options.items = items;
        ...
    });
</script>

The variable items is included in another JavaScript script that is also included in the same .jsp file:

<script type="text/javascript" src="<c:url value="/js/items.js"/>"></script>

items.js has the following structure:

var items = [{...}, {...}, {...}];

Now, the Servlet that implements doGet for this .jsp gathers some data from a database (and this data can be different every time) and uses it to write in the disk the file items.js mentioned above.

The problem is that the server (tomcat7) doesn't see that items.js has changed until it is restarted, but I need to generate that file every time because the data to gather is not always the same. So I'd like to know how to properly provide the first JavaScript function I mentioned with the data on items without having to restart the server. Of course, I want to avoid using scriptlets if possible.

Please note that I can't just delete that piece of JavaScript included in the beginning of this message because that piece of code is using a JavaScript library which I need to use to visualize my data.

Thanks in advance for your help.

6
  • 1
    Implement some sort of service which returns items as a json, instead of including the data as a js file. Then you can access it via ajax whenever you want. Commented May 18, 2014 at 20:33
  • 1
    Have a look at How do I disable tomcat caching? I'm having weird static file problems Commented May 18, 2014 at 21:06
  • You don't need to start start the server again. Simply delete the application cache folder in /work/Catalina/localhost. Commented May 18, 2014 at 21:09
  • 1
    You could also serve the .js file yourself instead of relying on the DefaultServlet. I would recommend against rolling your own DefaultServlet, though, for this purpose. Why do you need to persist the generated file to disk? Commented May 18, 2014 at 21:29
  • Sacho, that sounds good, I'll try it. Braj, that doesn't work because my file isn't "static" and when I start the server the first time it can't read my newly created JavaScript script (until the next server restart). @ChristopherSchultz, I certainly don't need to store that file anywhere, but I don't know how could I serve that file, I'm not quite sure what you mean. Thanks for your answers! Commented May 18, 2014 at 21:54

2 Answers 2

1

Adding a bit modification to your own solution, if you want to avoid scriptlet you can write your above line like this:

instead of

options.items = <%= request.getAttribute("items") %>;

write

options.items = ${requestScope.items}; or options.items = ${items};
Sign up to request clarification or add additional context in comments.

Comments

0

I'm replying to my own question to explain how I solved this problem.

I didn't do exactly what Sacho and Christopher Schultz suggested but what they said gave me an idea.

Firstly, on my servlet, I built a StringBuffer containing the data I want. Initially I wanted to use a JSONArray but that would mean using several transformations so that my js script could read (it'd be doable but it wouldn't simply consist of using the standard parser but something a little bit more complex). Then, I added this StringBuffer to the request parameter. This is how it all looks like:

protected void doGet(HttpServletRequest request,
                    HttpServletResponse response) {
    StringBuffer sb = new StringBuffer();
    // Fill the StringBuffer
    request.setAttribute("items", sb);
    request.getRequestDispatcher("/WEB-INF/myjsp.jsp").forward(
                            request, response);
}

Finally, I accessed this attribute from my jsp file as it follows:

options.items = <%= request.getAttribute("items") %>;

I wanted to avoid scriptlets but I think this piece of code is okay plus it's quite simple to implement.

Thanks for your help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.