1

I have constant values in one of Constant.java file and I want to access that constant value to jsp file. How is it possible? Actually I want to set that constant value to some checkbox and rediobutton value so that modification in single place can be possible.

I know one another way is use bundle. A properties file but I want to access it from constant file.

Ketan Bhavsar
  • 5,338
  • 9
  • 38
  • 69
  • 1
    possible duplicate of [How to reference constants in EL?](http://stackoverflow.com/questions/3732608/how-to-reference-constants-in-el) – BalusC Sep 05 '12 at 12:16

2 Answers2

4

Basically, you only need to import you Constant class :

<%@ page import="YourConstantClass"%> 
<%= YourConstantClass.YOUR_CONSTANT %>
Doug
  • 382
  • 1
  • 4
  • I believe the OP is asking this in context of "modern JSP/EL" (JSP 2.0, as introduced almost a decade ago) wherein *scriptlets* are strongly discouraged. Otherwise the answer is indeed extremely obvious. – BalusC Sep 05 '12 at 12:14
  • Without creating any object , how you can access members.That must be Static. – JDGuide Sep 05 '12 at 12:21
0

If you really want to do using script let tags then, you can use

Create an object of that class.And make your constant public.

   <%
    MyClass objClass=new MyClass();

    System.out.println(objClass.YOUR_CONSTANT);
    %>


   OR 

   <%=objClass.YOUR_CONSTANT%>

Now , you can use the constant value any where in the page.And if you do not want to create any object ,then better make the member static.

JDGuide
  • 6,239
  • 12
  • 46
  • 64
  • Don't suggest to instantiate objects just for the sake of reading a constant. [Doug's answer](http://stackoverflow.com/a/12281172/2223027) has the same amount of lines and is more efficient. – pyb Mar 13 '15 at 16:53