0

This question might sound similar to

JSP or JavaScript equivalent to PHP's $_SERVER["HTTP_HOST"]?

But the answers to that questions are only about finding out HTTP_HOST.

I am wondering if it is possible to create Sever-wide global variables in JSP.

Example:

User 1 computes some user independent function X. It is likely that other users too might visit the application to compute X. One way would be I can save it in mySQL and for subsequent user requests do a db lookup. But I dont want to use any DB and my data should persist only during the lifetime of the server.

In PHP I could simple set $_SERVER['x]=value;

I was wondering if similar thing can be achieved in JSP.

Community
  • 1
  • 1
  • JSP applications are persistent in-memory and you use the Java language in them, so the nature of your question suggests you're unfamiliar with Java development (because Java has class static members, and JSP has caching and state management). Can you provide more details about what you're wanting to accomplish in practice? – Dai Jun 04 '13 at 02:48

3 Answers3

1

The short answer is that you can use what is called Application Scope. This scope will persist since the application deployment until the container is stopped or the application redeployed.

In Servlets you can use it like this :

ServletContext context = request.getSession().getServletContext();
String value = "test";

// Set value in application scope
context.setAttribute("x",value);

// Get value from application scope
value = (String)context.getAttribute("x");

In Scriplets you can use it like this (using them is a bad practice) :

// Set value in application scope
application.setAttribute("x",value);

// Get value from application scope
value = (String)application.getAttribute("x");

In EL you can retrive value like this :

${applicationScope['x']}

Comming from a PHP world you will have to know that in that example I've used String object, but you can use any object. You can't use primitive data types.

Not related :

  • If you are working on a new project, you should consider moving to JSF 2.X, JSP is deprecated since 2009.
  • Read some Java EE tutorials
Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • @MrMoose _As JSP is a deprecated view technology since JSF 2.0 at December 2009 [...]_ [source](http://balusc.blogspot.ca/2013/01/apache-shiro-is-it-ready-for-java-ee-6.html) – Alexandre Lavoie Jun 04 '13 at 03:58
  • Yup, you are right. I was merely pointing out that it is considered deprecated for JSF only. He goes on to say - "It does indeed nowhere explicitly say that "pure" JSP at its whole own is deprecated for Java EE. Oracle is this way likely trying to push JSF forward.". [Another comment](http://stackoverflow.com/questions/4845032/wheres-the-official-jsp-tutorial/4847062#comment21032931_11804187) on the same question illustrates JSF != JSP. I wasn't meaning to critisise your answer, but only clarify. – Mr Moose Jun 04 '13 at 04:03
  • Good point :) I don't know since when I don't use JSP anymore, like JSF! hehe – Alexandre Lavoie Jun 04 '13 at 04:04
  • Unfortunately we are maintaining what we built in 2008. – Akshar Prabhudesai Jun 04 '13 at 22:59
  • @AksharPrabhudesai I know what it is, good thing is Java support deprecated for long time if we compare with PHP for example :) – Alexandre Lavoie Jun 05 '13 at 00:16
  • @AksharPrabhudesai Was this answer helping you? – Alexandre Lavoie Jun 07 '13 at 21:06
  • @AlexandreLavoie Yes. setAttribute/getAttribute is what I did eventually. I generally accept answers the next time I visit the site to post another question. – Akshar Prabhudesai Jun 14 '13 at 00:08
0

It is HttpServletRequest Interface on - javax.servlet.http.HttpServletRequest

This has most methods. Please check this link and let know if you have any questions.

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeader%28java.lang.String%29

Thanks.

RGV
  • 732
  • 3
  • 10
0

I think the simplest way to achieve this is by simply using static members of any class.