in Education by
This question already has answers here: How to reference constants in EL? (12 answers) Closed 6 years ago. 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" %> <%-- Do somthing --%> 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 , but again I'm struggling to find the correct syntax. UPDATE: Further to the suggestion below, I tried: 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 <%= "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..... JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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" %>

Using scriptlets

Constants.ATTR_CURRENT_USER

<%=Constants.ATTR_CURRENT_USER%>

Session[Constants.ATTR_CURRENT_USER]

<%=session.getAttribute(Constants.ATTR_CURRENT_USER)%> <%-- JSTL --%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Using JSTL

Constants.getATTR_CURRENT_USER_FUNC()

Session[Constants.getATTR_CURRENT_USER_FUNC()]

Constants.ATTR_CURRENT_USER

cons.ATTR_CURRENT_USER

--%>
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

Related questions

0 votes
    This question already has answers here: How to reference constants in EL? (12 answers) Closed 6 years ago ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    Accessing computer without prior authorization is a cyber-crimes that come under _______ (a) Section 65 (b) Section 66 ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    I have a Hello-World application with one java class and one jsp. The JSP prints out some text ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    I have a Hello-World application with one java class and one jsp. The JSP prints out some text ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Does anyone know of a good tool for debugging JSPs from within Eclipse? I'd like to be able to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm trying to use MappingJacksonJsonView with Spring 3.0, without success. I don't know what I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which page directive should be used in JSP to generate a PDF page? (a) contentType (b) generatePdf (c) typePDF (d ... JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which tag should be used to pass information from JSP to included JSP? (a) Using tag (b) Using tag (c) Using ... , JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which one is the correct order of phases in JSP life cycle? (a) Initialization, Cleanup, Compilation, Execution (b) ... & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which one of the following is correct for directive in JSP? (a) (b) (c) (d) This question was addressed to me ... , JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of the following action variable is used to include a file in JSP? (a) jsp:setProperty (b) jsp:getProperty ... JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Can and be used alternatively in JSP? (a) True (b) False The question was asked in an online interview. My ... JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Java code is embedded under which tag in JSP? (a) Declaration (b) Scriptlet (c) Expression (d) Comment This ... JSP & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I have a jsp with two drop down location and department. The value of department dropdown is populated ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Which page directive should be used in JSP to generate a PDF page? (a) contentType (b) generatePdf ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...