0

I have set an environment variable in windows by name "sample". Can I get this environment variable's value using below code?

System.getProperty("sample");

in UAT environment I have LINUX OS. Can I set the same environment variable in LINUX OS and retrieve the same using System.getProperty("sample")?

I mean does System.getProperty("sample"); retrieves irrespective of the operating system?

Also, I would like to get the environment variable's value in JSP.I know it is not recommended to keep logic in jsp but still, I need to use it. Can I use System.getProperty("sample"); in jsp? Please help me.

Hardik Doshi
  • 67
  • 1
  • 3
  • 16
user1016403
  • 12,151
  • 35
  • 108
  • 137

2 Answers2

16

Use System.getenv() to retrieve environment variables, not System.getProperty(). The latter is for (as the name implies) system properties.

What's the difference? Java system properties and environment variables


If you need to access an environment variable in a JSP, you're doing something wrong. At the very least, use a servlet. More reading: How to avoid Java code in JSP files?

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Hi Matt, Thanks for your reply. what is difference between system properties and environment variables? Thanks! – user1016403 May 05 '12 at 15:39
1

yes this is irrespective of OS and you can use it in JSP.

Satya
  • 8,693
  • 5
  • 34
  • 55