skip to main content

Programming Guides : Servlets & CGI : Developing COBOL Servlets

Developing COBOL Servlets
Note - the approach described here is deprecated. Veryant recommends the EIS solution as best practice to create COBOL Servlets. See COBOL Servlet option (OOP) for details.
 
The process for developing a COBOL servlet can be very similar to that for developing a CGI program. However, it really depends on what the HTML, Javascript or other Web programming code requires.
Simple COBOL Servlet
Here is a simple COBOL servlet that takes a CGI form field, NAME, as input and sends an HTML response with "Hello NAME" as output:
 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"
 identification division.
 object.
 procedure division.
 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.
      set out to res:>getOutputStream()
   catch exception
   end-try.
   set name-field to req:>getParameter"("NAME").
      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.
Basic Structure of the COBOL Servlet Class
As you can see from the example above, 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.
2.
http://www.w3.org/TR/html4/interact/forms.html#h-17.13.1.
3.
4.
5.
NOTE - It may be desirable to put all of the 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 limit the amount of OOCOBOL syntax that is required in the CALLed COBOL subprograms.
Compiling the COBOL Servlet
In order to compile the COBOL servlet, the class path must include a jar file containing the servlet API. This jar file is generally named servlet-api.jar and is located in the servlet container's installation lib directory. servlet-api.jar contains the javax.servlet.* classes.
Using Tomcat 7 add C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar to the class path. For example:
This will produce a single class file Sample.class that will be "deployed" to the servlet container.
The Web Application
The Servlet 2.4 Specification defines a web application as a collection of servlets, JSP pages , HTML documents, and other web resources which might include image files, compressed archives, and other data. A web application may be packaged into an archive or exist in an open directory structure.
Our simple COBOL servlet might be deployed in a web application with the following directory structure and files:
There are two files to create in addition to the COBOL servlet class; 1) index.html test page and 2) WEB-INF/web.xml deployment descriptor
index.html Test Page
This is a test web page used to test the servlet. You may also put any kind of web content in this page for users to see if they try to access the servlet directly through its URI.
WEB-INF/web.xml Deployment Descriptor
The WEB-INF/web.xml file contains all information the servlet container needs to know about the servlet.
A web.xml file will look something like the following:
<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <servlet>
      <servlet-name>SampleServlet</servlet-name>
      <servlet-class>Sample</servlet-class>      
    </servlet>
    <servlet-mapping>
      <servlet-name>SampleServlet</servlet-name>
      <url-pattern>/blue</url-pattern>
    </servlet-mapping>
    <filter>
      <filter-name>CobolFilter</filter-name>
      <display-name>CobolFilter</display-name>
      <description>CobolFilter</description>
      <filter-class>com.iscobol.web.IscobolFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>CobolFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  <listener>
   <listener-class>
   </listener-class>
  </listener>
</web-app>
The above web.xml file can be copied and used to test any COBOL servlet. It is only necessary to edit the <servlet> and <servlet-mapping> elements.
NOTE - The com.iscobol.web.IscobolFilter Class

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left