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.
- Create a new Java class as before. Put it in the
com.microfocus.book package, and give it the name
BookInterface.
- 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).
- Click
Edit > Find/Replace to find the
BOOK_FILE definition:
private static final String BOOK_FILE =
- 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.
- Click
File > Save.
Specifying the Java Build Path
To ensure that the
BookInterface.java does not contain errors you need to add the COBOL JVM runtime system library and the CobolBook project to the build path.
To do this:
- In Project Explorer, right-click the
JSPBookDemo project, and then click
Properties.
This opens the
Properties for JSPBookDemo dialog box.
- Click
Java Build Path, and then click the
Projects tab.
- Click
Add.
This opens the
Required Project Selection dialog box.
- Click
CobolBook.
- Click
OK.
- Click the
Libraries tab, and then click
Add Library.
This opens the
Add Library dialog box.
- Click
COBOL JVM Runtime System, and then click
Next.
- Click
Finish.
- Click
Apply and Close.
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.