Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:57:47+00:00 2026-06-01T21:57:47+00:00

Simply, how do I create a global variable in JSP, such that I can

  • 0

Simply, how do I create a global variable in JSP, such that I can access it across other JSP pages and/or inside frames/iframes? I tried <%!..%> but I got an error that the variable could not be resolved in a separate jsp page. Is it even possible to access JSP variables in more than one page without resorting to query strings, session variables, etcetera?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:57:49+00:00Added an answer on June 1, 2026 at 9:57 pm

    As I’ve already commented, you can use ServletContext to maintain the variables for all your application. Do not confuse this with static variables, because the ServletContext will die when your application is undeployed but static variables will be alive until the JVM is turned off.

    You can save variables in ServletContext by using setAttribute method. Also, you can get the actual value by using getAttribute. Let’s see a sample of ServletContext in a servlet:

    public class SomeServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            ServletContext servletContext = getServletContext();
            String someAttribute = servletContext.getAttribute("someAttribute");
            System.out.println("someAttribute value: " + someAttribute);
        }
    }
    

    Also, you can use a Listener to ServletContext, so you can execute some code when the application starts (is deployed correctly) to initialize the attributes on the ServletContext) and when it finish (before its undeployed).

    public final class MyAppListener implements ServletContextListener {
        public void contextInitialized(ServletContextEvent event) {
            System.out.println("Application gets started.");
            ServletContext servletContext = event..getServletContext();
            servletContext.setAttribute("someAttribute", "Hello world!");
        }
    
        public void contextDestroyed(ServletContextEvent event) {
            System.out.println("Application has finished.");
        }
    }
    

    If you’re using Java EE 5, you should configure the listener in the web.xml

    <listener>
        <listener-class>mypackage.listener.MyAppListener</listener-class>
    </listener>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I create or use a global variable inside a function? How do
what i'm trying to do this this. Simply create a C# windows app that
I'm simply trying to create a UML diagram where I can show the header
I wish to create a very simple web app that simply displays an Image(s)
I'm trying to create an app for Android that simply sends a command to
I am trying to create a very simple chat window that simply has the
Possible Duplicate: Why can't I create an array with size determined by a global
Someone once said : a global variable is really a variable you create to
Visual Studio 2008 w/Sp1 To reproduce my problem I simply create a new .Net
I'm trying to create simply connect with ActiveMQ using JNDI. I have: Queue named

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.