In the last article Creating an Effective Development Environment we got your development environment ready to write Java applications for Liberty JVM servers in CICS.

Prior to going through this article, make sure your development environment is set up properly. If you’ve not already got a development environment set up, finish part one of this series:
Developing Liberty applications for CICS: Part 1 – Setting up an effective development environment.

Once you’ve done that, set up a Liberty JVM server in CICS to host the web application. If you’ve not set up a Liberty JVM server in CICS before, the article linked below, Starting a Liberty JVM server in 4 easy steps, will take you through all of the required steps.

After you’ve completed this article you can move on to the next one:
Developing Liberty applications for CICS: Part 3 – Deploying your application

Writing Your First Web Application

One of the greatest Strengths of Liberty JVM servers is how incredibly easy it is to write and deploy web applications. This article will take you through all the steps required to write a simple application, and next time we’ll show you how to deploy and call it.

This article will take you through the steps using the Eclipse environment set up in the previous article, however it is possible to use your own Eclipse environment, such as RDz, if you have one already set up. Please be aware that if you do choose to use your own environment, some steps may be different in your Eclipse environment.

Step 1 – Creating a project

In the previous article, we set up your Eclipse environment ready to write and deploy a web application, now it’s time to use it.

In your Eclipse environment, find the Project Explorer view, it’s usually on the left hand side of the screen. Right click anywhere in that view and expand the menu option for New then select Dynamic Web Project. If Dynamic Web Project is not listed in the menu, select other, then type dynamic web project in the filter box and select it from the list.

The  'Dynamic Web Project' wizard in the Eclipse menu.
The ‘Dynamic Web Project’ wizard in the Eclipse menu.

A dynamic web project is a simple web-enabled project, which will produce a Java artifact called a ‘Web Application Archive’ (or ‘WAR’ for short), one of several supported deployment containers for Liberty.

Give your dynamic web project a name, our tutorial series will use the name ‘liberty.tutorial’. Untick the box marked Add project to an EAR (We won’t be using an EAR for this project), you can use the default values for everything else, so just click Finish. Eclipse will then generate the project for you, and display it in your Project Explorer view.

If you’re working from an Eclipse environment other than the one described in the first article in this series, it may be worth checking your Java level in Eclipse. In some cases you may find your Eclipse environment has chosen Java 1.8 as it’s compile level. If your CICS region is running Java 1.7, applications compiled in your environment won’t run once they make it into CICS. To correct this, simply change the compiler level to Java 1.7. Any 1.7 Java applications will also run in Java 1.8.

Next we need to create a servlet in your dynamic web project. A servlet is a Java class enhanced with HTTP libraries, which allows it to be used as a web entry point for your web application.

Right click on your project, and expand the New option from the right click menu then select Servlet from the list. If Servlet does not appear in your list, select Other and use the filter box at the top of the window to help you find it.

You’ll be asked for a package name and class name. For the purpose of this tutorial we’ll use the package name liberty.tutorial and the class name SimpleServlet. Click the Finish button to create the skeleton class.

You may notice that you have a few red highlights in your code at this point. Eclipse by default doesn’t know about the servlet and CICS libraries, so the next step is to add them to your project. Right click on the project and select properties.

In the properties windows, type Java Build Path into the filter box and select Java Build Path from the list. On the right hand side of the Java Build Path menu, select the tab called Libraries. This shows you the libraries currently available to your project. On the right hand side, select the button labelled Add Library…

The build path menu's library tab
The build path menu’s library tab

From the Add Library dialogue box select Liberty JVM server libraries and then click Next. Select your version of CICS from the drop down menu in the next window, then click Finish. After you’ve done that, the red highlights should disappear from your dynamic web project.

The CICS JVM server libraries in the 'Add Library' menu.
The CICS JVM server libraries in the ‘Add Library’ menu.

Step 2 – Writing the web application code

Open up the java source code for SimpleServlet. You’ll have find three automatically generated methods already in the class. The first is a constructor, which allows you to add in code which will run whenever the servlet is created. The remaining two methods implement HTTP methods, specifically HTTP GET and HTTP POST.

We don’t need to make changes to the constructor or doPut() method, we’ll only be adding code to the method called doGet().

‘doGet()’ is called every time we send a HTTP GET request to the servlet and any code in the method will be executed for each request.

* @see HttpServlet#doGet(HttpServletRequest request,
*                        HttpServletResponse response)
protected void doGet(HttpServletRequest request, HttpServletResponse response){
      // TODO Auto-generated method stub

* @see HttpServlet#doPost(HttpServletRequest request,
*                        HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response){
      // TODO Auto-generated method stub

We’re going to use this servlet to create a very simple ‘HelloWorld’ style application that uses CICS. In the doGet() method add the following code:

ServletOutputStream out = response.getOutputStream();
Task cicsTask = Task.getTask();
out.println("Hello from CICS task " + cicsTask.getTaskNumber());

The ServletOutputStream allows us to create a response body to send back to the requester. The Task class allows us to get some basic information from CICS, in this case we’re going to get the task number that we’re running under in CICS, and then output that to the response body.

That’s all you need to add! Your web application is now ready to run in CICS. In the next article we’ll take you through all of the steps required to deploy your web application.

1 comment on"Developing Liberty applications for CICS: Part 2 – Writing your first web application"

  1. will try this out!

Join The Discussion

Your email address will not be published. Required fields are marked *