4

I started following one tutorial about JSTL tags. The repositories tutorial used are too old, so I wanted to try it my way using some newer versions. Unfortunately, I am stuck now. I will tell you what I did, so I hope, you can help me out.

I downloaded 2 jar files from these two links: api, implementation.

Jar files I got, I copied inside WebContent/Web-INF/lib. Now if I do this, I get an error:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<body>

<c:set var="varA" value="hello world" />
<%=varA %> //varA cannot be resolved to a variable

</body>
</html>

To be honest I have no idea why we need import statement when jar files are literally in my project's lib folder. Also, does anyone know what is wrong here and why editor can't find my variable varA?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Stefan
  • 969
  • 6
  • 9

1 Answers1

5

The variables set using JSTL tags can not be accessed directly with a scriptlet. There can be two ways to access varA.

1. Using JSTL tags:

<c:set var="varA" value="hello world" />
<c:out value="${varA }"></c:out>

2. Using pageContext.getAttribute:

<c:set var="varA" value="hello world" />
<%=pageContext.getAttribute("varA")%>
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • You are right. Can you tell me why do I need import statement at line 1 as well? Wasn't it enough that I copied jar files inside a lib folder? – Stefan Dec 05 '20 at 22:05
  • I do not see any `import` statement in your question. – Arvind Kumar Avinash Dec 05 '20 at 22:09
  • I meant on this `<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>`. How do you call this one then? :) And my question is why we need that line. – Stefan Dec 05 '20 at 22:10
  • 1
    @Stefan - This is called the **taglib directive**. This is the way to tell the application server that you are going to use a taglib (e.g. the statement in your question informs the application server that you are going to use tags from the JSTL **core** taglib. Check [this](https://www.oreilly.com/library/view/head-first-servlets/9780596516680/ch09s39.html) to learn more about it. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Dec 05 '20 at 22:15
  • Thanks! And do you know why I can't access variables declared in JSTL it in "normal" scriplets? – Stefan Dec 05 '20 at 22:21
  • @Stefan - You can access them but not directly. The second option in my answer is the way to access it with a scriplet. It's because the compiler can not see an attribute declared as a string literal. – Arvind Kumar Avinash Dec 05 '20 at 22:22
  • Yeah, I tried code, you are correct. But why I can't do it simply directly? :/ – Stefan Dec 05 '20 at 22:24
  • @Stefan - As I have mentioned above, JSTL allows a variable to be declared as a string literal but java compiler cannot see it as the compiler never looks into the string literals (i.e. a valid string inside the double quotes). That can be discovered only by the runtime engine. Feel free to comment in case of any further doubt. – Arvind Kumar Avinash Dec 05 '20 at 22:30