0

How to access the jstl attribute "difpos" in jsp varaiable "ph".

<c:set var="difpos" value="10"/>

<%int ph = pageContext.getAttribute("difpos"); out.println(ph);%>

I am getting the error in eclipse as "Type mismatch: cannot convert from Object to int"

I tried as below

<%int ph = (Integer)pageContext.getAttribute("difpos");

the error is cleared, but output error as

"java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap')"

1 Answers1

-1

What we are doing here is accessing a "scoped variable" in a scriptlet. The following is an example JSP. Scriptlets are very powerful. A lot of people think that they are evil. See JSP - What is wrong with scriptlets, and what to use instead

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="difpos" value="10"/>
<%String ph =(String)pageContext.getAttribute("difpos"); out.println(ph);%>
<c:set var="difpos2" value="${Math.abs(12)}"/>
<%Long ph2 =(Long)pageContext.getAttribute("difpos2"); out.println(ph2);%>
<c:set var="difpos3" value="${Math.PI}"/>
<%Double ph3 =(Double)pageContext.getAttribute("difpos3"); out.println(ph3);%>

Output: 10 12 3.141592653589793

rickz
  • 4,324
  • 2
  • 19
  • 30