Dynamically put JSP tag on a JSP page-Collection of common programming errors
JSP, as a server-side technology, can’t take part in runtime events (i.e. after user loaded the page); once the rendered response (i.e. HTML) leaves the server and embarks on a journey to user’s browser, JSP‘s role is over. If you want to change the content, you can only get it by making another request.
There are several technologies that can help you with assembling the HTML response from smaller building blocks (e.g. JSP includes, Apache Tiles, etc.), but by definition they can only react to request parameters that are known up front, at request time.
If you need runtime flexibility, you can either:
- always build a “full” page that contains all potential widgets and return it to all users but make some of the elements initially hidden (via CSS), user’s request for a widget would simply make it visible
- request HTML fragments from the server via AJAX and attach them to the page using JavaScript.