Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Servlet/JSP

  Asked By: Daryl    Date: Jun 20    Category: Java    Views: 995
  

How can I declare global variable in Servlet and JSP which can be
accessed by all other Servlets / JSP present in the same container

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Harold Graham     Answered On: Jun 20

I'm not a JSP/Servlet expert, but try cookies or static variables.

 
Answer #2    Answered By: Giovanna Silva     Answered On: Jun 20

U can Use the ServletContext or HttpSession to store your
variables

For Example..
HttpSession session;
session = req.getSession(true);
session.setAttribute("user_name",userName);

Hope this info will help U .

 
Answer #3    Answered By: Aaliyah Khan     Answered On: Jun 20

Using session will only be "global" within each user.
The ways that will make it truly global  for each user,
is the application object or any other number of ways
involving the servlet  configuration.

 
Answer #4    Answered By: Maurice Hanson     Answered On: Jun 20

this is what the HttpSession is for. Use the setAttribute method. If you
want to share the
variable across several contexts make sure that you have enabled this in
your server settings.

 
Answer #5    Answered By: Bellona Lopez     Answered On: Jun 20

Sorry. i mean to say in context-param tags in the web.xml file.

 
Answer #6    Answered By: Jonathan Harrison     Answered On: Jun 20

use a bean in application scope

i.e. <jsp:useBean id="global" class="com.mybeans.Global" scope="application"
/>
then any variables inside that bean can be seen in any jsp.
i.e. String str = global.getEmployeeName();

 
Answer #7    Answered By: Gerald Cruz     Answered On: Jun 20

U can define ur parameter/ values in the web descriptor file, in the
init-param tags.

 
Answer #8    Answered By: Zoar Mizrachi     Answered On: Jun 20

You can create a class with getter and setter for a variable  which you
wants to access globally and get the variable from any servlet.

 
Didn't find what you were looking for? Find more on Servlet/JSP Or get search suggestion and latest updates.




Tagged: