Search code examples
jspjstlelscriptlet

How to convert dynamic scriptlets to JSP 2.0 with JSTL/EL?


I need to grab a dynamically-named variable off of the request. This works in scriptlet form, but I'd rather not clutter up the page with scriptlets.

<%
    String requestValueKey = "something_" + request.getParameter("State") + "_" + request.getParameter("UUID");    
    String requestValue = request.getParameter(requestValueKey);
%>

I'd like to switch it to JSTL but I can't figure out how to come up with a dynamically named session value key that relies on other values in the session.


Solution

  • Use <c:set> to prepare the dynamic key and use brace notation [] to get a value by a dynamic key.

    <c:set var="requestValueKey" value="something_${param.State}_${param.UUID}" />
    

    Then you can get it by ${param[requestValueKey]} in remnant of the page.