Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8408903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:45:36+00:00 2026-06-09T23:45:36+00:00

Working on a program and trying to print a static integer from another class.

  • 0

Working on a program and trying to print a static integer from another class. The weird thing is I can do it with scriptlets, but not with JSTL. Check out the error checking code I just wrote.

Comments: <%=Comments.getCommentCount() %> <br />
Comments: ${Comments.getCommentCount()} <br />
Comments: <c:out value="${Comments.getCommentCount()}" /> <br />
Comments: <c:out value="1" />

This gives me an HTML output of

Comments: 5 <br />
Comments:  <br />
Comments:  <br />
Comments: 1

So as you can see only the first and last lines of code work. How can I print out this static variable without scriptlets?

And in my header I have

import="org.test.Comments"

Comments.java

package org.test;

import java.util.ArrayList;
import java.util.Collections;

public class Comments 
{
    private String name = "";
    private String comment = "";
    private static ArrayList<String> allComments = new ArrayList<String>();


    public void setNewComment(String name, String comment)
    {
        this.name = name;
        this.comment = comment;
        allComments.add(getComment());
    }

    public static ArrayList<String> getCommentList()
    {
        Collections.reverse(allComments);
        return allComments;
    }


    public static int getCommentCount()
    {
        return allComments.size();
    }


    public String getComment()
    {
        return String.format("Name:   %s <br />Comment:  %s <p><hr /></p>", name, comment);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:45:37+00:00Added an answer on June 9, 2026 at 11:45 pm

    You need not call the getter inside jstl. just do this

    <c:out value="${Comments.commentCount}" /> 
    

    assuming your variable name is commentCount and not CommentCount.

    It would work even without <c:out>

    Comments: ${Comments.commentCount} <br />
    

    but using <c:out> would be better, to avoid cross-site scripting as explained here

    UPDATE

    In the class you have mentioned, there is no field with the name commentCount. So it wouldn’t work. You can use use jsp fn tag to get a the size of a collection directly inside the jsp.

    Include this in the header

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

    and then do this:

      <c:out value="${fn:length(allComments)}" /> 
    

    or

     Comments: ${fn:length(allComments)} <br />
    

    This should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone tell me why my program is working weird. I am trying to
I'm trying to print call stack from within a program. Unfortunately, call to glibc
I'm trying to get a simple send and receive UDP program working, but I'm
I am stuck on why my program is not working, I am trying to
I'm trying to get a simple winsock program working, so I create my socket
I'm trying to get a helloworld type program working with REST CI/jquery. I've included
I am working on a PHP / MySQL stat logging program and am trying
I am using Junit 4. My whole program is working fine. I am trying
I am working through trying to learn to program in Python and am focused
I'm working on database migration program and I'm trying to migrate one database to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.