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

  • Home
  • SEARCH
  • 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 60047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:06:03+00:00 2026-05-10T18:06:03+00:00

I have a class that defines the names of various session attributes, e.g. class

  • 0

I have a class that defines the names of various session attributes, e.g.

class Constants {     public static final String ATTR_CURRENT_USER = 'current.user'; } 

I would like to use these constants within a JSP to test for the presence of these attributes, something like:

<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %> <%@ page import='com.example.Constants' %>  <c:if test='${sessionScope[Constants.ATTR_CURRENT_USER] eq null}'>     <%-- Do somthing --%> </c:if> 

But I can’t seem to get the sytax correct. Also, to avoid repeating the rather lengthy tests above in multiple places, I’d like to assign the result to a local (page-scoped) variable, and refer to that instead. I believe I can do this with <c:set>, but again I’m struggling to find the correct syntax.

UPDATE: Further to the suggestion below, I tried:

<c:set var='nullUser' scope='session' value='${sessionScope[Constants.ATTR_CURRENT_USER] eq null}' /> 

which didn’t work. So instead, I tried substituting the literal value of the constant. I also added the constant to the content of the page, so I could verify the constant’s value when the page is being rendered

<c:set var='nullUser' scope='session' value='${sessionScope['current.user'] eq null}' /> <%= 'Constant value: ' + WebHelper.ATTR_CURRENT_PARTNER %> 

This worked fine and it printed the expected value ‘current.user’ on the page. I’m at a loss to explain why using the String literal works, but a reference to the constant doesn’t, when the two appear to have the same value. Help…..

  • 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. 2026-05-10T18:06:03+00:00Added an answer on May 10, 2026 at 6:06 pm

    It’s not working in your example because the ATTR_CURRENT_USER constant is not visible to the JSTL tags, which expect properties to be exposed by getter functions. I haven’t tried it, but the cleanest way to expose your constants appears to be the unstandard tag library.

    ETA: Old link I gave didn’t work. New links can be found in this answer: Java constants in JSP

    Code snippets to clarify the behavior you’re seeing: Sample class:

    package com.example;  public class Constants {     // attribute, visible to the scriptlet     public static final String ATTR_CURRENT_USER = 'current.user';      // getter function;     // name modified to make it clear, later on,      // that I am calling this function     // and not accessing the constant     public String getATTR_CURRENT_USER_FUNC()     {         return ATTR_CURRENT_USER;     }   }     

    Snippet of the JSP page, showing sample usage:

    <%-- Set up the current user --%> <%     session.setAttribute('current.user', 'Me'); %>  <%-- scriptlets --%> <%@ page import='com.example.Constants' %> <h1>Using scriptlets</h1> <h3>Constants.ATTR_CURRENT_USER</h3> <%=Constants.ATTR_CURRENT_USER%> <br /> <h3>Session[Constants.ATTR_CURRENT_USER]</h3> <%=session.getAttribute(Constants.ATTR_CURRENT_USER)%>  <%-- JSTL --%> <%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %> <jsp:useBean id='cons' class='com.example.Constants' scope='session'/>  <h1>Using JSTL</h1> <h3>Constants.getATTR_CURRENT_USER_FUNC()</h3> <c:out value='${cons.ATTR_CURRENT_USER_FUNC}'/> <h3>Session[Constants.getATTR_CURRENT_USER_FUNC()]</h3> <c:out value='${sessionScope[cons.ATTR_CURRENT_USER_FUNC]}'/> <h3>Constants.ATTR_CURRENT_USER</h3> <c:out value='${sessionScope[Constants.ATTR_CURRENT_USER]}'/> <%-- Commented out, because otherwise will error: The class 'com.example.Constants' does not have the property 'ATTR_CURRENT_USER'.  <h3>cons.ATTR_CURRENT_USER</h3> <c:out value='${sessionScope[cons.ATTR_CURRENT_USER]}'/> --%> <hr /> 

    This outputs:

    Using scriptlets

    Constants.ATTR_CURRENT_USER

    current.user

    Session[Constants.ATTR_CURRENT_USER]

    Me


    Using JSTL

    Constants.getATTR_CURRENT_USER_FUNC()

    current.user

    Session[Constants.getATTR_CURRENT_USER_FUNC()]

    Me

    Constants.ATTR_CURRENT_USER


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

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can add the DataSourceController as a resource (you'll have… May 11, 2026 at 10:02 am
  • added an answer afx_msg still exists but has always been purely informative. A… May 11, 2026 at 10:02 am
  • added an answer There are two routes you can take. 1) Rip it… May 11, 2026 at 10:02 am

Related Questions

I have a class that defines the names of various session attributes, e.g. class
I have a class that defines a CallRate type. I need to add the
I have a few tables that I've defined like the below examples: class TableA
I have the following members defined in a class that I'm trying to deserialise:
I have a class that defines a read-only property that effectively exposes a private
I have an actionscript file that defines a class that I would like to
I have a python module that defines a number of classes: class A(object): def
I have a class that I want to use to store properties for another
I have a class that map objects to objects, but unlike dictionary it maps
I have a class that I wish to expose as a remote service using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.