Scriptlet and JSTL variable sharing

I know that mixing scriplets and JSTL in JSP is a bad practice, but sometimes you can’t avoid it and every time I do it I can’t remember how to share variables between scriplets and JSTL so this post is a sort of reminder for the future. Hope it can be useful for other forgetful persons like me :-)

Access scriptlet variable with JSTL

String myVariable = "Test";
pageContext.setAttribute("myVariable", myVariable);

<c:out value="myVariable"/>

Access JSTL variable with scriptlet

<c:set var="myVariable" value="Test"/>

String myVariable = (String)pageContext.getAttribute("myVariable");