session tracking


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: session tracking

  1. #1
    Join Date
    Feb 2005
    Posts
    31

    session tracking

    Hi there,

    I have a strange problem you may be able to shed some light on. At least I hope so. I'm working with an existing project using JSP's and Servlets and a backend MySQL database. The project entails the ability to perform ad hoc queries to the database. The source code files can be found at the bottom of the page at:

    http://www.eas.asu.edu/~cse494db/Ion...BCExample.html

    The project contains one servlet called Contol.java and a bean called QueryBean.java. The Control servlet is first invoked using a POST call. In doing so, a Session object is created and an instance of QueryBean is created and stored in the Session object. The QueryBean contains the meta data for the database. A jsp called QueryInput.jsp loads and at the same time also activates a second browser window loading MetaTables.jsp. When MetaTables.jsp loads in the second window it calls
    QueryBean qb = (QueryBean)session.getAttribute("qBean");
    from the session object retrieving the QueryBean instance and the meta data is used to be displayed in a table. The first time this process occurs everything is fine, until I dismiss the window containing MetaTables.jsp.

    Here's what happens...QueryInput.jsp has a button on it. It is used to re-display the window that MetaTables.jsp loads into if the user dismisses that window when it first loads. Here's where the interesting part comes in. If I click on the button in the main browser window containing QueryInput.jsp the second browser window appears alright, but for whatever reason all the data that was in the QueryBean instance is gone. MetaTables.jsp loads and attempts to obtain the QueryBean instance again from the Session object, but there is no data to display. It's gone!

    What's going on? Does anybody know?

    Alan

    ***************QueryInput.jsp*************************

    <%@ page import = "java.io.*" %>
    <%@ page import = "java.sql.*" %>
    <%@ page import = "com.components.QueryBean" %>
    <jsp:useBean id="qBean" class="com.components.QueryBean" scope="session"/>

    <%
    String ManagerOnBoard = (String)session.getAttribute("ManagerOnBoard");
    if(ManagerOnBoard == null)
    {
    %>
    <HTML>
    <HEAD>
    <TITLE>MainMenu</TITLE>
    <link href="site.css" rel="stylesheet" type="text/css">
    </HEAD>
    <body>
    <h1>ACCESS DENIED</h1>
    </body>
    </html>
    <%
    }
    else
    {
    %>
    <HTML>
    <head>
    <link href="site.css" rel="stylesheet" type="text/css">
    <script language=javascript>
    function MetaData()
    {
    window.open('MetaData.jsp','','toolbar=no,resizable=yes,menubar=no,location=no,height=400, width=400');
    }

    ...
    ...

    </script>

    </head>
    <BODY onLoad="MetaData()">
    <b><center><h2><jsp:getProperty name="qBean" property="dbName"/> Database </h2></center></b>
    <hr>
    <form action=MainMenu.jsp target =_parent>
    <p><input type="SUBMIT" VALUE="Return to Main Menu"></p>
    </form>
    <b>SQL Select/Insert/Update/Delete:</b><br>
    <table><tr><td>
    <form name="MyForm" action="/scholastic/Control" method="GET" TARGET="Output" onsubmit="return checkForm(this)">
    <p><textarea name="query" rows=9 cols=40>
    </textarea></p>
    </td><td><p><input type="SUBMIT" VALUE="Submit">
    </p><p><input type="RESET" name="clear" VALUE="Clear">
    </p><p><input type="button" value="Open MetaData" onClick="MetaData()">
    </form>

    </td>
    </tr>
    </table>
    </BODY>
    </HTML>
    <%
    }
    %>



    **********************MetaTables.jsp*****************


    <%@ page import = "java.io.*" %>
    <%@ page import = "java.sql.*" %>
    <%@ page import = "java.util.*" %>
    <%@ page import = "com.components.QueryBean" %>
    <!--jsp:useBean id="qBean" class="com.components.QueryBean" scope="session"/-->
    <%
    QueryBean qb = (QueryBean)session.getAttribute("qBean");
    Vector tables = (Vector)qb.getTables();
    String ManagerOnBoard = (String)session.getAttribute("ManagerOnBoard");

    if(ManagerOnBoard == null)
    {
    %>
    <HTML>
    <HEAD>
    <TITLE>MainMenu</TITLE>
    <link href="site.css" rel="stylesheet" type="text/css">
    </HEAD>
    <body>
    <h1>ACCESS DENIED</h1>
    </body>
    </html>
    <%
    }
    else
    {
    %>
    <HTML>
    <head>
    <link href="site.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <!--jsp:getProperty name="qBean" property="dbName"/-->
    <center><h2> <%=qb.getDbName()%> Database MetaData</h2></center>
    <%
    for(int i =0; i< tables.size(); i++)
    {
    %> <table><tr>
    <%
    StringTokenizer token = new StringTokenizer((String)tables.get(i));
    String tableName = ((String)token.nextToken()).toUpperCase();
    %>
    <th><a href="/scholastic/Control?tableName=<%=tableName%>" target=Output><%=tableName%></a></th><th> :</th>
    <%
    while(token.hasMoreTokens())
    {
    %>
    <th><%=token.nextToken()%></th>
    <%
    }
    %>
    </tr></table>
    </body>
    </html>
    <%
    }
    }
    %>

  2. #2
    Join Date
    Feb 2005
    Posts
    31
    Here's the Control.java file. I had to send it seperatly due to the system.


    ******************Control.java************************


    package com.components;

    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import javax.sql.*;
    import javax.naming.*;

    public class Control extends HttpServlet
    {
    Connection connection = null; // the connection to the database
    Statement statement = null; // a statement object for the queries
    String dbName; // the database name
    public QueryBean qBean = null;

    private ServletContext context; // objects used to transfer control to the jsp pages

    /*Initializing the servlet*/
    public void init(ServletConfig config) throws ServletException
    {
    super.init(config);
    context = config.getServletContext();
    }

    /*
    The doPost method handles POST requests
    This method is accessed first in from the time the user will
    enter a datasource name in the main page.
    The function will first establish a connection to the database
    with the requested data source name. If the data
    source does not exist then the control is tranferred to an error page
    with a corresponding message. If the database exists
    then we save time by getting all the meta-data from the database
    ( table names, and columns) and storing them in the
    bean. The bean is stored in the session object and control is
    transferred to the query page which loads the two frames. One
    for entering the query and one for results. This function is
    accessed only whenever we are in the main page.
    */

    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {
    ResultSet rsTables = null; //ResultSet objects for the database tables and columns
    ResultSet rsColumns = null;
    //databse metadata object to access the meta-data from the database
    DatabaseMetaData dbmd = null;
    qBean = new QueryBean(); // the bean object to store the information
    //the database source name entered from the user
    dbName = "scholastic_db";
    //store the database name in the query bean so pages can diplayed it later.
    qBean.setDbName(dbName);
    String tableName;

    // The code below gets the database table names and
    // columns and stores them in the bean
    try
    {
    Context ctx = new InitialContext();
    if(ctx == null )
    throw new Exception("No Context available...");

    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Scholastic");

    if (ds != null)
    {
    connection = ds.getConnection();
    if(connection != null)
    {
    HttpSession session = req.getSession(true);
    dbmd = connection.getMetaData();
    rsTables = dbmd.getTables(null, null, null, null);
    while (rsTables.next())
    {
    StringBuffer buff = new StringBuffer();
    tableName = rsTables.getString("TABLE_NAME");
    if (rsTables.getString("TABLE_TYPE").equals("TABLE"))
    {
    buff.append(tableName + " ");
    rsColumns = dbmd.getColumns(null,null,tableName,null);
    while (rsColumns.next())
    {
    buff.append(rsColumns.getString("COLUMN_NAME") + " ");
    }
    qBean.setTables(buff.toString());
    }
    }
    // put the bean in the session
    session.setAttribute("qBean", qBean);
    // transfer control to the query page
    res.sendRedirect("/scholastic/manager/Query.jsp");
    rsTables.close();
    rsTables = null;
    rsColumns.close();
    rsColumns = null;
    connection.close();
    connection = null;
    }
    }
    }
    catch (Exception e)
    {
    // If there is an error with the database source
    // transfer control to an error page
    try
    {
    connection.close();
    connection = null;
    }
    catch(SQLException sqle){;}
    res.sendRedirect("/scholastic/manager/errorDbSource.jsp");
    }
    finally
    {
    // Always make sure result sets and statements are closed,
    // and the connection is returned to the pool
    if (rsTables != null)
    {
    try { rsTables.close(); } catch (SQLException e) { ; }
    rsTables = null;
    }
    if (rsColumns != null)
    {
    try { rsColumns.close(); } catch (SQLException e) { ; }
    rsColumns = null;
    }
    if (connection != null)
    {
    try { connection.close(); } catch (SQLException e) { ; }
    connection = null;
    }
    }
    }
    ...
    ...
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center