You may have heard recently that we announced the readiness of the SAP Web IDE Full-Stack version, and that we recommend migrating your projects to it from the former version of SAP Web IDE.
This is great news because SAP Web IDE Full-Stack contains new and exciting capabilities, one of them being Java development.
In this blog series, I will show you how to develop a simple Java servlet in SAP Web IDE Full-Stack, covering all aspects of the software development lifecycle.
This blog describes the steps necessary to Develop a simple “Hello World” servlet which produces a WAR file containing a basic “Hello World” application.
The life-cycle continues in Part 2, where you will learn how easy it is to Build and Test the generated application.
Finally, in Part 3 I will describe how to Run and Debug the application. I will also show you our new Hot Deployment feature.
Let’s get started!
Let’s start by creating a new MTA project with a Java module.
MTA stands for Multi-Target-Application and it is SAP’s approach for developing full-stack applications that can be comprised by various modules.
One of the big advantages of MTA is the one, common lifecycle of the app.
Please refer to this SAP document for more information.
- Create an MTA project by clicking File→New→Project from Template
- Select Multi-Target Application and click Next
- Name the project mtaproj, and then click Next and FinishNow, let’s create a Java module.
A Java module is a collection of related Java files and service definitions.
Java modules implement the business logic of your application.
A Java module can be packaged either as a Java Web Archive (WAR) or a Java Archive (JAR) built with Apache Maven.
- Right-click the project you just created and select New→Java Module.
SAP Web IDE Full-Stack provides several Java module types depending on your application needs.
For more information on the available types, see Developing Java Modules.
- Select the Simple Web Application template and then click Next
- Enter web as the module name, and click Next
- Enter Group ID, Artifact ID and Package, or just use the default values, then click Finish.
A new web folder is generated under your mtaproj project. The folder represents a simple web application with a standard Maven directory layout. It also contains a HelloServlet.java file representing a sample HTTP Servlet which writes “Hello World” as its output.
You can also add your own Java Class, Enum or Interface by right-clicking any sub-folder under the Java Module.
Let’s extend our auto generated Servlet code to also print out the HTTP response status.
The SAP Web IDE Full-Stack code editor includes our Code Completion feature.
This is how it works:
- Open the java file.
- Jump to line 28 and start typing “int iStatus = response.set“.
- Press <CTRL>+<Space>.
A popup showing all the available Java method of type HttpservletResponse is displayed.
- Hover over the first option – setStatus(int sc, String..).
A detailed description of the function is displayed.
- Select the first option and complete the line – setStatus();
- The editor now displays syntax errors because we did not provide the relevant parameters for our new method.
- We can see more detailed information regarding the error by opening the Problems view. Learn more about the Problems view.
- Click on the Problems view button on the right-side pane. The error from line 28 is displayed
- We can fix the error quickly using the Autofix
- Right-click on the first line and select the first option – Change to ‘getStatus(..)’
- Line 28 of the HelloServlet.java file is updated to response.getStatus();
- A new warning message appears stating that the iStatus variable is not used
- To fix that we add a new line after line 36: writer.write(“\n\nStatus is: ” + iStatus);
- This adds also the response status to our application’s output.
We have created a new MTA project with a Java module containing an HTTP Servlet application.
We have also started to edit the code using the code editor and fixed it using the Problems view and Autofix features.
In our next part of the series, we will compile our web Java Module by using the Build option.