1

I have a jsp page(calendar) with lots of JSTL tags in it. I set the attributes in my servlet and get them in my jsp page thanks to JSTL, EL.

When I press nextweek, I open a xmlhttp which sends a GET to my servlet(Ajax). All my attributes renew so i want to get them again in my jsp page. I do not want to dispatch the servlet to my jsp page because of performance latency. I don't want to fetch servlet results because they are attributes.

I just want to refresh my JSTL & EL so they will get the new values (without refreshing the page).

Is this an illogical way of thinking? but anyway, how can I refresh my JSTL,EL, scriptlets so the new values will appear?

RobinHo
  • 565
  • 1
  • 9
  • 24

2 Answers2

3

I just want to refresh my JSTL & EL so they will get the new values (without refreching the page)

This is impossible. Note that EL and JSTL run on server side in view build time, so once they're applied when generating the server response, they can't be updated in the page until the server generates new content using the view (basically, your JSP with JSTL, EL and other components)1.

You should look into AJAX requests to your servlet (or the controllers you're using) and probably handling a JSON response to resolve the behavior of your JSP page.

More info:

1 Scriptlets also fall in server side category but I omit them since you should not use them for being highly discouraged to use in modern Java web development. More info How to avoid Java code in JSP files?

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I have a <:Foreach items="${allhours}" which contains a big 2D Arraylist with lots of objects in it.. there is no way to get the new values in the arraylist without dispatching from servlet to jsp or sending them manually trough xmlhttp serverresponse(ajax)? – RobinHo Oct 02 '13 at 15:09
  • @RobinHo as said in my answer and in SotiriosDelimanolis', **no**. The only way you could re-generate it without a page-refresh-like behavior is through ajax requests to your server and handling the response to update the DOM. – Luiggi Mendoza Oct 02 '13 at 15:13
  • @RobinHo note that there are Java web application frameworks like JSF 2.0 that implements ajax and help users to easily do this kind of work. – Luiggi Mendoza Oct 02 '13 at 15:14
  • @RobinHo To add to what Luiggi just said. Instead of returning JSON in your response, you can return (controlled) HTML and append that (or replace completely) some part of the DOM. – Sotirios Delimanolis Oct 02 '13 at 15:31
  • @SotiriosDelimanolis I don't recommend doing this since breaks the MVC pattern: let the view handle the view logic (HTML DOM, JavaScript animations, apply CSS, image rendering, etc.), let the controller handle controller logic. – Luiggi Mendoza Oct 02 '13 at 15:35
  • Meh; it only barely breaks MVC in any meaningful way, allows re-use of existing logic/templates, and allows devs to move to more client-side functionality one chunk at a time. It's a useful stopgap measure. – Dave Newton Oct 02 '13 at 15:41
1

HttpServlets, JSP, JSTL, EL, and scriptlets are all server side components, ie. get executed on the server to produce an HTTP response. Javascript and AJAX are client side components, ie. work on the returned HTTP response.

You cannot refresh my JSTL & EL on the client side because they simply do not exist.

A possible (and common) solution is to have the request you make with AJAX produce a JSON response which you use to populate/replace HTML elements, where your EL had previously been used to set a value.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724