solutions: Innovative & Affordable COBOL
Take more control of your applications with powerful COBOL and Java technology solutions from Veryant
 

Taking Your COBOL to the Web ‘Servlet-Style’

Servlets offer a streamlined deployment model over CGI for server-side implementations

isCOBOL Evolve brings the benefits of the Java platform to COBOL applications without rewriting code and retraining programmers. isCOBOL compiled programs are 100% portable and run on any device that supports a Java Virtual Machine. This provides an ‘instant’ solution for thin client deployments, but what if your business is looking for an even lighter weight approach? If you are looking to expose COBOL business logic directly to a browser or Web Service, deploying COBOL programs as Java servlets provides a convenient way to add custom functionality without changing back- end program code. This article briefly examines some of the differences between using CGI and servlets to deploy COBOL applications to the Web and includes an example to illustrate how COBOL code can be deployed in a servlet environment.
Historically, many businesses elected to use CGI programming to bring COBOL applications to the Web. Although this approach leverages existing back-end business logic, CGI-based solutions result in platform-specific implementations with high overhead that require a lot of code on the front end to pass stateful information within an application.
In addition to Java technology's platform independence and promise of write once, run anywhere, servlets offer several advantages over CGI programs:
  • Servlets are persistent. Loaded only once by a Web server, servlets inherit processing state between invocations and use concurrency control. Servlets can maintain services such as database connections between requests.

  • Servlets are fast. Servlets handle concurrent requests on multiple threads rather than in multiple processes and remain in memory to be reused when called.

  • Servlets are flexible. Platform and Web server independent, servlets can be used with a variety of clients, not just Web browsers.

  • Servlets are secure. Because servlets are invoked through a web server, business logic is not directly exposed to the client. In addition, servlets are isolated from each other so that an error in one servlet cannot corrupt any other servlet.

  • Servlets are open. Servlets can be used with a variety of client-side and server-side Web programming techniques and languages.
The isCOBOL Runtime Framework goes one step further with servlets by using the concurrency capabilities of servlets to associate a user session with a COBOL thread context. This makes sure that the same instances of COBOL programs get used each time they are called during a particular user session. In other words, COBOL programs called during a particular user session retain their file states and working-storage data between requests from that user session. If desired, the programmer can cancel the program at any time with the CANCEL statement.

Dish up your COBOL with Servlets

So what specifically does a COBOL programmer used to the CGI approach need to know in order to get some hands-on experience with servlets? The process for developing COBOL servlets can be very similar to that of developing CGI programs.
Here is a simple COBOL servlet developed with isCOBOL that takes a CGI form field, NAME, as input and sends an HTML response with "Hello NAME" as output:

*> Sample servlet that takes a NAME field and outputs "Hello <NAME>"
class-id. Sample          as "Sample" inherits HttpServlet.
configuration section
repository
class HttpServlet         as "javax.servlet.http.HttpServlet"
class HttpServletRequest  as "javax.servlet.http.HttpServletRequest"
class HttpServletResponse as "javax.servlet.http.HttpServletResponse"
class ServletOutputStream as "javax.servlet.ServletOutputStream"
.
*> Object Methods 
identification division.
object
.
procedure division
.

*> doPost: Invoked when a POST type request occurs
identification division.
method-id. doPost             as "doPost" override.
data division.
working-storage section.
77 name-field                pic x any length.
77 out                       object reference ServletOutputStream.
linkage section.
77 req                       object reference HttpServletRequest.
77 res                       object reference HttpServletResponse.
procedure division using req res.
main.
   try
      set out to res::"getOutputStream"
   catch exception
      goback
   end-try.
   set name-field to req::"getParameter"("NAME").
   try
      out::"println"('<html>' as string)
      out::"println"('<head>' as string)
      out::"println"('<title>Sample</title>' as string)
      out::"println"('</head>' as string)
      out::"println"('<body>' as string)
      out::"print"('Hello ' as string)
      out::"print"(name-field as string)
      out::"println"('</body>' as string)
      out::"println"('</html>' as string)
   catch exception
      continue
   end-try.
 end method.
 end object.

As you can see from this example, object oriented COBOL (OOCOBOL) syntax is used to make the COBOL servlet class. There are 5 characteristics of this class that are common in servlets designed to replace CGI programs:
  1. The servlet class "inherits" javax.servlet.http.HttpServlet which allows it to plug-in to any standard servlet container (i.e. web server that supports servlets)

  2. It defines a doPost method override which takes as parameters the HttpServletRequest and HttpServletResponse object references. Servlets can also override the doGet method, or can override both doPost and doGet. These correspond to the GET and POST CGI methods. For example, doPost will be called if the HTML has <FORM method="post" ... >. For more information about get and post see the "Form submission method" section of the W3C HTML specification at http://www.w3.org/TR/html4/interact/forms.html#h-17.13.1.

  3. The servlet class invokes the HttpServletResponse getOutputStream method to save the ServletOutputStream object in a variable named "out".

  4. It invokes the HttpServletRequest getParameter method to get the value of the CGI field.

  5. And finally it invokes the ServletOutputStream print and println methods to send the output back to the client.
Don't let this OOCOBOL scare you. The key parts do not change from servlet to servlet. You can copy/paste the code from the example above and use it again and again without learning additional object programming. Most tasks can be accomplished with just the HttpServletRequest and ServletOutputStream objects. You can put all of this OOCOBOL syntax in the COBOL servlet class alone and have the COBOL servlet class CALL other COBOL subprograms that do not have OOCOBOL syntax in them. Or you can limit the amount of OOCOBOL syntax that is required in the CALLed COBOL subprograms.
If you have an existing CGI program then you can pass in the HttpServletRequest and ServletOutputStream objects and make straightforward changes so you can use the CGI program for the most part as is. For example,
  1. Get the CGI fields using the servlet request object getParameter method.

    set external-form-item-field-1 to
        servlet-request-obj::"getParameter"("FIELD_1")
    set external-form-item-field-2 to
        servlet-request-obj::"getParameter"("FIELD_2")

  2. Change every DISPLAY UPON SYSOUT or DISPLAY external-form-item to code which will construct the output and send it using the output stream object println method.

    invoke servlet-output-obj "println" using
        html-output-line as string
        on exception continue
    end-invoke

Of course, prior to deploying servlets, your environment will need to be configured to support servlets. Veryant has tested and recommends the Apache Tomcat 6. For additional information on servlets and deployment guides, email info@veryant.com.

Learn more on the Web

There are also many excellent resources on the Web for learning about Java servlets and CGI programming. Just a few such links have been included below to help provide a foundation of information that can be built upon to learn how to develop and deploy COBOL servlets with isCOBOL Evolve.
  back
 

Solution Areas
  Overview
  COBOL Migration Services
  Globalization, Localization
  Java Integration
  Java Servlets
  Web Enablement


...We can now provide our software on any platform that supports the Java Runtime Environment – meaning more flexibility for traditional desktop deployment models, as well as the possibility of delivering enhanced thin client and Web 2.0 distributions down the road..."
 
Rolf Ahlrichs, programmer, HAY Computing


Join our Community
Keep updated on the latest product news and information
  Sign up here


 
 

© Veryant - All Rights Reserved
Veryant and isCOBOL are trademarks or registered marks of Veryant in the United States and other countries. All other marks are the property of their respective owners.