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.
Servlet Sessions, and variable scope.
Tomcat - Configuring Web Applications example from a Well House Consultants training course
More on Tomcat - Configuring Web Applications [link]

This example is described in the following article(s):
   • Variable scope in Java Servlets and other web applications - [link]

Source code: Autumn.java Module: A653
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Autumn extends HttpServlet{

/* For the moment, push the GET and POST requests to the
same internal method */


        public static int peeps = 0; // Visitor count
        public static int latest = 0; // Latest value across threads

        public void doGet (HttpServletRequest inp,
                        HttpServletResponse outp)
                        throws ServletException, IOException {
                dojob(inp,outp);
                }
        public void doPost (HttpServletRequest inp,
                        HttpServletResponse outp)
                        throws ServletException, IOException {
                dojob(inp,outp);
                }

/* This application notes the integer that each user enters of each
page and keeps a separate running total for each of the users via a
session. (Trivial application ... replace "Integer" by "Things added
to shopping cart so far" object later on for a practical program! */


        protected void dojob (HttpServletRequest inp,
                        HttpServletResponse outp)
                        throws ServletException, IOException {

/* Get any data off the incoming form */

                int aok = 0; int dim1 = 0;
                try {
                        dim1 = Integer.parseInt(inp.getParameter("quant"));
                        aok = 1;
                } catch (Exception e) {
                        // Set up a sensible error string here
                        }

                outp.setContentType("text/html");
                PrintWriter out = outp.getWriter();

                int report = latest;

/* Here is the session stuff ... all together for this first demo */
        // Get the current session (i.e. what the visitor left last time)
                HttpSession who = inp.getSession(true);
        // Get the total so far that was saved in previous visits
                Integer runtot = (Integer)who.getAttribute("sofar");
                if (runtot == null) {
                        runtot = new Integer(0);
                        peeps++;
                        }
        // Add in any new value
                if (aok == 1)
                        runtot = new Integer(runtot.intValue() + dim1);
                        latest = dim1;
        // Save back into session
                who.setAttribute("sofar",runtot);
/* That's it for the session stuff */

        out.println("<head><title>Summing across requests</title></head>");
        out.println("<body><h1>Session based adder</h1>");

/* Illustration of scope of variables
Iteration level - for this current server access only
        e.g. temportary variables / echoing back immediate user inputs
        achieved by local variables in doGet or doPost
User/Session level - for this user across all their accesses in this session
        e.g. variables that the user is maintaining state with (shopping cart)
        achieved by session object
Application level - for all users across all session
        Overall variables - e.g. top bid, number of people, latest sale price
        achieved by static variables

NOTE ... need to save away / possibly reload variables if servlet may reload

Also note - to share variables between servlets, probably best to pick them
up from a database OR use multiple service levels with a common worker task
on the back server.
*/


                if (aok > 0) {
                        out.print("The total so far is ");
                        out.print(runtot);
                        out.print(" units [USER/SESSION level]<hr>");

                        out.print("The previous value ANYONE said was ");
                        out.print(report);
                        out.print(" units [APPLICATION level]<hr>");

                        out.print("The number of unique people is ");
                        out.print(peeps);
                        out.print(" people [APPLICATION level]<hr>");

                        out.print("The number this time is ");
                        out.print(dims);
                        out.print(" people [ITERATION level]<hr>");

                } else {
                        // Initial error message is poor ...
                        // More explanation / help needed by user!
                        out.print("Bitova Issue");
                }

        /* Make up a repeat form to try with more data */
                out.print("<br><form method=POST>");
                out.print("Rows <input name=quant><br>");
                out.print("<input type=submit></form>");

                out.println("</body>");
                }

                }
Learn about this subject
This module and example are covered on the following public courses:
 * Deploying Apache and Tomcat
 * Deploying Java Applications on Linux / Unix
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering Lamp deployment are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Tomcat - Configuring Web Applications" training module. You'll find a description of the topic and some other closely related examples on the "Tomcat - Configuring Web Applications" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Web site author
This web site is written and maintained by Well House Consultants.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 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/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb