Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is the difference when using JSTL and JSP?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).

What is Jstl what are its benefits over JSP Scriptlets?

The JSP Standard Tag Library (JSTL) is a very important component released by Oracle for JSP programming. JSTL allows us to program our JSP pages using tags, rather than the scriptlet code that most JSP programmers are already accustomed to. JSTL can do nearly everything that regular JSP scriptlet code can do.

What is JSTL and its advantages?

Advantage of JSTL Fast Development JSTL provides many tags that simplify the JSP. Code Reusability We can use the JSTL tags on various pages. No need to use scriptlet tag It avoids the use of scriptlet tag.


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 var="test" value="test1"/>
<%
  String resp = "abc";
  String test = pageContext.getAttribute("test");
  resp = resp + test;
  pageContext.setAttribute("resp", resp);
%>
<c:out value="${resp}"/>

If you look at the docs for <c:set>, you'll see you can specify scope as page, request or session, and it defaults to page.

Better yet, don't use scriptlets at all: they make the baby jesus cry.


@skaffman nailed it down. They live each in its own context. However, I wouldn't consider using scriptlets as the solution. You'd like to avoid them. If all you want is to concatenate strings in EL and you discovered that the + operator fails for strings in EL (which is correct), then just do:

<c:out value="abc${test}" />

Or if abc is to obtained from another scoped variable named ${resp}, then do:

<c:out value="${resp}${test}" />