Java EE Servlets, Apache Tomcat 8 - Calculator Project



Hello everyone,

In this post, I am going to talk about a Java Servlet Calculator Project that can perform some mathematical operations with the help of Servlets.
In the early days, web servers delivered static contents that were indifferent to users' requests. However, this has started to change by the help of Servlets. Java Servlets are server-side programs (running inside a web server) that can handle clients' requests and return a customized or dynamic response for each request. The dynamic response could be based on user's input (e.g., search, online shopping, online transaction) with data retrieved from databases or other applications, or time-sensitive data (such as news and stock prices).

Servlets typically run on the HTTP (HyperText Transfer Protocol). HTTP is an asymmetrical request-response protocol. The client sends a request message to the server, and the server returns a response message as illustrated.
HTTP Overview

Server-Side Technologies

Today, there are many server-side technologies available: Java-based (servlet, JSP, JSF, Struts, Spring, Hibernate), ASP, PHP, CGI Script, and many others.
Java Servlet is the foundation of the Java server-side technology, JSP (JavaServer Pages), JSF (JavaServer Faces), Struts, Spring, Hibernate, and others, are extensions of the Servlet technology.

A servlet's life cycle is managed via the init(), service() and destroy() methods.
Servlet's Lifecycle

Apache Tomcat Server

Servlets are server-side programs run inside a Java-capable HTTP server. Apache Tomcat Server (@ https://tomcat.apache.org) is the official Reference Implementation (RI) for Java servlet and JSP, provided free by open-source foundation Apache (@ https://www.apache.org).

In order to install Apache Tomcat Server using eclipse, go to Servers tab, right click -> New Server and find Apache Tomcat v8 and proceed with the download and the installation.

After successfully installing the Tomcat Server, now we can create our project and start writing the code. First of all, let's create the project in Eclipse:

- File --> New --> Dynamic Web Project
- Enter the project name and select Apache Tomcat as the Target runtime
- Leave the configuration as default.
- Create/ add the html / image files under the Web Content folder
- Create the java files under Java Resources --> src


After all these configurations, we need to create an index page for our website. (index.html)

index.html

Then the user interface of the calculator can be designed in a separate html file (inputform.html):

inputform.html
Now, the remaining part is to create the Servlet to process the data inserted to these textfields. We implement the functionality in doGet(..) method of our Servlet. In order to determine which mathematical operation has been selected by the user, we need to read the name of this parameter using the
"HttpServletRequest request" and its getParameter(" ..") method. Then we can create a switch-case architecture and process the request.



public class CalculatorServlet extends HttpServlet implements SingleThreadModel {

 //....

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String operation = request.getParameter("operation");
         // .....
     
        }
  // ...
}

After successfully completing the code, the result should be as follows,
Output
The project source code is accessible from the button below.
Click to View or Contribute

For more information on Java EE ->

Please leave a comment if you have any questions or comments. 


Author:

Software Developer, Codemio Admin

Disqus Comments Loading..