I can pass a variable from a JSP scriptlet to JSTL but not from JSTL to a JSP scriptlet without an error

Scripts are raw java embedded in the page code, and if you declare variables in your scripts, then they become local variables embedded in the page. In contrast, JSTL works entirely with scoped attributes, either at page, request or session scope. You need to rework your scriptlet to fish test out as an attribute: <c:set … Read more

Use EL ${XY} directly in scriptlet

You’re mixing scriptlets and EL and expecting that they run “in sync”. That just won’t work. The one is an oldschool way of writing JSPs and the other is a modern way of writing JSPs. You should use the one or the other, not both. Coming back to the concrete question, under the hoods, EL … Read more

How do I pass JavaScript values to Scriptlet in JSP?

I can provide two ways, a.jsp, <html> <script language=”javascript” type=”text/javascript”> function call(){ var name = “xyz”; window.location.replace(“a.jsp?name=”+name); } </script> <input type=”button” value=”Get” onclick=’call()’> <% String name=request.getParameter(“name”); if(name!=null){ out.println(name); } %> </html> b.jsp, <script> var v=”xyz”; </script> <% String st=”<script>document.writeln(v)</script>”; out.println(“value=”+st); %>

How to evaluate a scriptlet variable in EL?

So you want to evaluate a scriptlet variable in EL? Store it as a request attribute. <% String var = “some”; request.setAttribute(“var”, var); %> <c:if test=”${param.variable1 == ‘Add’ && var == ‘some’}”> However, this makes no sense. You should avoid scriptlets altogether and use JSTL/EL to prepare this variable. So if you make the functional … Read more

How to avoid using scriptlets in my JSP page?

I think it helps more if you see with your own eyes that it can actually be done entirely without scriptlets. Here’s a 1 on 1 rewrite with help of among others JSTL (just drop jstl-1.2.jar in /WEB-INF/lib) core and functions taglib: <%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %> <%@ taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn” %> <html> <head> <title>My … Read more

How can I avoid Java code in JSP files, using JSP 2?

The use of scriptlets (those <% %> things) in JSP is indeed highly discouraged since the birth of taglibs (like JSTL) and EL (Expression Language, those ${} things) way back in 2001. The major disadvantages of scriptlets are: Reusability: you can’t reuse scriptlets. Replaceability: you can’t make scriptlets abstract. OO-ability: you can’t make use of … Read more