Creating a Session Management Class

This part of the tutorial details the most relevant class for invoking a JVM COBOL program from a JSP Web application. This class handles session management and integration into a Java environment. It also handles the interaction between the view bean (BookJsp.jsp) and the COBOL program's smart details linkage class, which was generated when you used the ILSMARTLINKAGE compiler directive previously.
  1. Create a new Java class as before. Put it in the com.microfocus.book package, and give it the name BookInterface.
  2. Overwrite the skeleton contents of the file with the BookInterface.java file that you downloaded previously (the file is stored in C:\myfiles\COBOL_JSP_DEMO_2_2_SRC\JSPBookDemo\src\com\microfocus\book).
  3. Click Edit > Find/Replace to find the BOOK_FILE definition:
    private static final String BOOK_FILE =
  4. Change the path to match the location of the bookfile.dat file that you downloaded previously.
    Note: Windows users must use a forward slash to delineate folder names; using a back slash results in an error.
  5. Click File > Save.

Comments

The sample code contains some important concepts, which are explained here. Note the two constructors listed below:

    
  public BookInterface(HttpSession session)
  {
    this(ServletRunUnitManager.getManager().GetSessionRunUnit(session));
  }
  
  public BookInterface(IRunUnit runUnit)
  {
    this.runUnit = runUnit;
    BookLegacy bookLegacy = (BookLegacy) runUnit.GetInstance(BookLegacy.class);
    
    if(bookLegacy == null)
    {
      bookLegacy = new BookLegacy();
      runUnit.Add(bookLegacy);
    }
    
    this.bookLegacy = bookLegacy;
  }
These constructors handle two separate functions:
  • The first constructor deals with the servlet's HTTP session object (HttpSession) and its run unit manager (ServletRunUnitManager).
  • The second constructor handles the linking of this interface class to a run unit, and gets the program instance of BookLegacy.
The interaction here is: get the ServletRunUnitManager (which is a singleton) and then call GetSessionRunUnit with the session object. The manager will create a run unit if this is the first call with this particular session. It will also manage the shutdown handling routines, so that when this session is invalidated the manager will call StopRun on the run unit attached to that session. In summary, this short section of code manages both program isolation and session life cycles.
Note also that the function parameter has been split into separate method calls:
public BookBean readBook(String stockNo) throws JavaBookException
public BookBean addBook(BookBean book) throws JavaBookException
public BookBean deleteBook(String stockNo) throws JavaBookException
public BookBean nextBook(String stockNo) throws JavaBookException
This style ensures compact and discrete functions, and is good coding practice.