-1

in my scenario i have a code like below:

     <c:forEach items="${dbEntries}" var="c" varStatus="loop">
                    <tr> 
                          <% 
                              int i = 0;
                              System.out.println(i);
                          %>
                          <td rowspan="1">${c.getRh_name()}</td>                                 
                          <td rowspan="1">${c.getIpm_name()}</td>` 
                    </tr>
      </c:forEach>

now i want to access the variable from html. like below

<h1>${i}</h1>

but its not displaying.can anyone help??

9
  • you can set i varilbe in request scope request.setAttribute("variableName",i) and access <h1>${variableName}</h1>. <h1> tag is out of the foreach or? Commented Mar 22, 2018 at 8:41
  • @GurkanYesilyurt where will i declare this request.setAttribute("variableName",i) . I mean in <% %> ?? Commented Mar 22, 2018 at 8:45
  • yes, expressions (EL) like ${anything} works with scopes (request, session etc.). If you access any variable you can set it in any scope before. Commented Mar 22, 2018 at 8:49
  • for this you can use jstl also. Commented Mar 22, 2018 at 8:55
  • @SanjayPatel can you write the code using jstl?? Commented Mar 22, 2018 at 8:58

1 Answer 1

0

Use jstl instead of java code inside jsp like this.

    <c:set var="salary" scope="session" value="${2000*2}" /> 
     <c:out value="${salary}" /> 
    <c:forEach begin="1" end="5" step="1">
         <c:set var="salary" value="${2000*2}" /> 
    </c:forEach> <c:out value="${salary}" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.