Java Server Pages Sharing Session and Application Data

The objects having request scope are available in all pages across a single request. However, sometimes objects should be shared among multiple requests.

Think about an online examination system. JSP pages requested from the same browser must share the same user name that was provided during the login procedure. This is required because different users have different sets of data such as expiry time, number of questions answered so far, number of correct answers, and so on. This type of information can be shared through session scope.

The objects having session scope are available to all pages requested by the same browser. The implicit object session is one such object. This session object can be used to store and retrieve other objects. So, objects may be shared across pages in the same session using this session object. The following is a code fragment used in login.jsp.

<!-login.jsp–>

<%

String user = request.getParameter(”user”);

String password = request.getParameter(”password”);

//verify the user here

double startTime = System.currentTimeMillis();

session.setAttribute(user, String.valueOf(startTime));

<jsp:forward page=”exam.jsp” />

%>

It stores the time when the user starts its session in the session object and forwards the request to exam.jsp. The exam.jsp will be called many times by the user. The exam.jsp retrieves the start time for this user, checks whether the user has exhausted its time limit, and takes necessary actions.

String userl = (String)session.getAttribute(”user”);

double startTime = Double.parseDouble((String)session.getAttribute(”user”));

double currentTime = System.currentTimeMillis();

if(currentTime – startTime > 600000) { //duration is 10 minutes

//the time is over, forward the request to the page logout.jsp

%>

<jsp:forward page=”logout.jsp” />

<%

}

%>

Now, think about the same online examination system where different users are giving the examination using different browser windows, but using the same database. For efficiency purposes, information should be shared among pages even if the pages are requested by different users. This type of information can be shared through application scope.

The objects that have application scope are shared by all pages requested by any browser. One such implicit object is application. Consider the following code fragment in the login.jsp page:

Object obj = application.getAttribute(”config”);

if(obj == null) {

ExamConfig conf = new ExamConfig();

application.setAttribute(”config”, conf);

}

It looks for an object having the name “config” in the application object. If no such object exists, it creates an ExamConfig object and stores one such object in the application object. When the ExamConfig object is created, connection to the database is established. This database connection can now be shared by all other pages related to this application.

Here is the code fragment used in the exam.jsp page.

ExamConfig conf = (ExamConfig)application.getAttribute(”config”);

Statement stmt = conf.getConnection().createStatement();

//use stmt to fire SQL query

All requests that use the exam.jsp file can simply use the same object stored in the application object to get the Statement object and fire the SQL query using JDBC technology.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *