Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Variable scope in Java Servlets and other web applications

How long does a variable last? When you set a variable in a Java Servlet (a Java programming that's running on a web server in a container such as Tomcat), the variable may ...
• be local to the block of code it's in, and be lost when that block has been completed
• be present only for the current request and response - in other words, only be available for a single browser call to the web server
• be present through a whole series of requests and responses made by an individual user in a linked series of operations
• be omnipresent within an application.
These are the possible variable scopes.

Let's see examples of them - let's say you are writing an online auction site (I know Ebay has done it already, by the way).
• the current top price of an item is an example of a global, omnipresent value
• your login details will be present through a whole series of requests and reponses, so that you can make a number of bids without having to give your information every time
• the information that you filled in on a form will be present throughout the running of a single request / response
• a whole series of temporary variables will be created and released as the server looks through its files to find any similar items you may wish to bid for, and generates a table of information about them.

During today's "Java on the Web" training, I wrote an example that shows each of the four scopes [source code]. The example is much simpler than an auction site - each user may simply enter a series of numbers, and the application keeps a running total for each user. It also tracks the most recent value entered by anyone, and the number of new sessions that have been started.

• The number of sessions and latest value entered are (in Java terms) static class variables (peeps and latest if you look at the code) as they last for the whole life of the web application and are omnipotent. I have described them being at Application level

• The running total for each user is (in Java terms) a session variable, as it lasts for the user's individual session, but each user has his/her own variable of this sort - if you look at the code, the example is called runtot. Unsurprisingly, I have described these variables as being at a Session or User level

• The request and response objects (inp and outp in my example) which are passed into the doGet and doPost methods are what I have described as being at an Iteration level

• finally .. variables declared within blocks of code (such as aok and dim1) could be very temporary indeed, reused time after time, and are at a local code level.

Why is scope important? Because you can loose vital information, or corrupt one user's data with another's, if you get it wrong. If everyone had a Session variable for the top bid to date, then everyone would win the auction. If an iteration variable that the user entered was stored in the session, then errors would not be automatically corrected. And if credit card details were kept at an application level not a session or user level, then the last person to enter his card details would be charged for everyone's purchases.
(written 2009-05-01, updated 2009-05-02)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J703 - Java - Variables
  [127] Conversion and coercion in Java - (2004-11-22)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
  [2153] Class Loading and Variable Conversion in Java - (2009-05-02)
  [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
  [3041] Java - basic rules for arithmetic, variables and conversion - (2010-11-10)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3365] Turning bright delegates into bright and knowledgable ones - (2011-07-21)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [4324] Learning to program - variables and constants - (2014-11-22)
  [4345] Incrementing a variable in Java - Pre and Post Increment - (2014-12-08)

Back to
A very easy JSP (Java Server Page)
Previous and next
Horse's mouth home
Forward to
Routing Network Traffic - Proxies, Redirects and DNS
Some other Articles
My four feet!
What is a JSP tag library?
Routers, Firewalls and multilayer servers
Routing Network Traffic - Proxies, Redirects and DNS
Variable scope in Java Servlets and other web applications
A very easy JSP (Java Server Page)
An evening excursion to Devizes and Avebury
Using the internet to remotely check for power failure at home (PHP)
Looking for a career change - Physician to Web Site Designer
Throughout the year, around the world
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2148_Var ... tions.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb