Aly Afifi
Aly Afifi

Reputation: 35

unable to print values of custom jsp tag <% %>

I have a problem with printing a variable in Jsp custom tags. When using this below code, c:out does not print anything and when trying to use the default attribute in c:out, it prints the value in default which means that the variable is null which it is not here is my code.

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <% int x = 1;%>
 <c:out value="${x}" />

HOw can i make this Work

Upvotes: 0

Views: 1180

Answers (1)

Bacteria
Bacteria

Reputation: 8616

If you want to print a declared variable in scriptlet tag, using c:out tag, then you can do it in the below mentioned way

Set the variable in the page context under a variable name and evaluated the value using EL expressions

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
</head>
<body>
<%
   int x = 1;
  //set variable x in the page context under the variable name "var_x"
  pageContext.setAttribute("var_x",x);
%>
<c:out value="${var_x}" />
</body>
</html>

For more details you can view this tutorial The most commonly used JSTL tag which is used to display the result of the expressions

Upvotes: 1

Related Questions