Introduction
Web services: Tomcat can also be used to host web services built using technologies such as SOAP or RESTful api. This allows developers to expose their web services to other applications and integrate them into larger systems.
Web services are software systems that enable communication and data exchange between different applications and platforms over the internet or a private network. A web service provides a standard way for two software systems to interact with each other and exchange data using open, industry-standard protocols.
Web services typically use a standardized messaging format such as XML or JSON to exchange data, and communication between systems is often facilitated through the use of HTTP, though other protocols such as SOAP and REST can also be used. Web services can be used for a wide variety of applications, including data exchange, data sharing, and application integration.
There are several types of web services, including:
- SOAP (Simple Object Access Protocol): SOAP is a messaging protocol that allows for the exchange of structured data between different applications.
- REST (Representational State Transfer): REST is an architectural style that uses HTTP to exchange data between different applications. RESTful web API services are designed to be lightweight and easy to use.
- XML-RPC (Remote Procedure Call): XML-RPC is a simple protocol that allows for remote procedure calls between different applications.
- JSON-RPC (Remote Procedure Call): JSON-RPC is similar to XML-RPC but uses JSON instead of XML for data exchange.
Web services have become an integral part of modern software systems, providing a way to integrate different applications and platforms and creating more powerful and flexible software solutions.
Here’s a step-by-step guide to setting up a RESTful API Tomcat-based application on CentOS 8:
- Install Java on CentOS 8 by running the following command:
#sudo dnf install java-1.8.0-openjdk
java-1.8.0-openjdk-devel epel-release
- Download the Tomcat 9 installation package from the Apache Tomcat website. You can do this by running the following command:
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.72/bin/apache-tomcat-9.0.72.tar.gz
Extract the Tomcat package by running the following command:
tar -xvf apache-tomcat-9.0.72.tar.gz
Move the Tomcat directory to the /opt directory by running the following command:
sudo mv apache-tomcat-9.0.59 /opt/tomcat
Create a Tomcat user by running the following command:
sudo useradd -r tomcat
Set the appropriate permissions by running the following commands:
sudo chown -R tomcat: /opt/tomcat/
sudo chmod +x /opt/tomcat/bin/*.sh
Start Tomcat by running the following command:
sudo su - tomcat -c /opt/tomcat/bin/startup.sh
Verify that Tomcat is running by accessing the Tomcat homepage in a web browser. Open a web browser and navigate to http://localhost:8080/. You should see the default Tomcat homepage.
Create a RESTful API web service by creating a Java class that implements the functionality of your web service. For example:
HelloServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><body>");
out.println("<p>Hello World! -- From UnixCop.com</p>");
out.println("</body></html>");
out.close();
}
}
Compile the Javascript with the command below.
# javac -cp /usr/share/java/tomcat/tomcat-servlet-api.jar HelloServlet.java
# ls -l HelloServlet.*
-rw-r--r--. 1 root root 795 Feb 23 13:06 HelloServlet.class
-rw-r--r--. 1 root root 558 Feb 23 13:05 HelloServlet.java
Create the “WEB-INF\classes” sub-directory under my application directory on the Tomcat server. Make sure that it has “read” permission for all users.
# cd /opt/tomcat/webapps/hello
# mkdir WEB-INF
# mkdir WEB-INF/classes
Save this Servlet bytecode file, HelloServlet.class, to the “WEB-INF\classes” sub-directory. Make sure that it has “read” permission for all users.
# cp HelloServlet.class /opt/tomcat/webapps/hello/WEB-INF/classes/
Map the Servlet to a URL path name on the Tomcat server by creating the “/opt/tomcat/webapps/hello/WEB-INF/web.xml” file. Make sure that it has “read” permission for all users.
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Verify that the RESTful web service works by accessing it in a web browser. Open a web browser and navigate to http://localhost:8080/{war_file_name}/hello, where “{war_file_name}” is the name of the WAR file that you deployed in step 11. You should see the “Hello, world!” displayed in the web browser.
Access the Servlet class with any browser through the mapped URL “http://localhost:8080/hello/hello” with any browser. The output of my first Servlet class shows up:
# curl http://localhost:8080/hello/hello
<html><body>
<p>Hello World! -- From UnixCop.com</p>
</body></html>