Build a Temperature Converter Maven Web Application Using Java

Temperature Converter Web Application Using Java, JSP

Build a Temperature Converter Maven Web Application Using Java, 

Temperature conversion is a common utility in many applications. In this tutorial, we’ll create a simple Maven-based web application that converts temperatures between Celsius, Fahrenheit, and Kelvin using Java, JSP, and EJB.


Step 1: Prerequisites

Before starting, ensure you have the following installed:

  • Java Development Kit (JDK)
  • Apache Maven
  • Apache Tomcat Server
  • An IDE like Eclipse or IntelliJ IDEA

Step 2: Set Up the Maven Project

  1. Open your IDE and create a new Maven project.
  2. Update the pom.xml file with the required dependencies:
<dependencies>  
    <dependency>  
        <groupId>javax</groupId>  
        <artifactId>javaee-api</artifactId>  
        <version>8.0</version>  
        <scope>provided</scope>  
    </dependency>  
</dependencies>  

Step 3: Create the Project Structure

Organize the project as follows:

src/main/java  
└── com.example.temperature  
    ├── ConverterEJB.java  
    └── ConverterServlet.java  

src/main/webapp  
└── WEB-INF  
    └── web.xml  
└── index.jsp  

Step 4: Develop the Backend Logic

1. Create the EJB (ConverterEJB)

package com.example.temperature;

import javax.ejb.Stateless;

@Stateless  
public class ConverterEJB {  
    public double toCelsius(double fahrenheit) {  
        return (fahrenheit - 32) * 5 / 9;  
    }  

    public double toFahrenheit(double celsius) {  
        return (celsius * 9 / 5) + 32;  
    }  

    public double toKelvin(double celsius) {  
        return celsius + 273.15;  
    }  
}  

2. Create the Servlet (ConverterServlet)

package com.example.temperature;

import java.io.IOException;  
import javax.inject.Inject;  
import javax.servlet.ServletException;  
import javax.servlet.annotation.WebServlet;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

@WebServlet("/convert")  
public class ConverterServlet extends HttpServlet {  
    @Inject  
    private ConverterEJB converter;  

    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        double input = Double.parseDouble(request.getParameter("temperature"));  
        String scale = request.getParameter("scale");  
        double result = 0;  

        switch (scale) {  
            case "Celsius":  
                result = converter.toCelsius(input);  
                break;  
            case "Fahrenheit":  
                result = converter.toFahrenheit(input);  
                break;  
            case "Kelvin":  
                result = converter.toKelvin(input);  
                break;  
        }  

        request.setAttribute("result", result);  
        request.setAttribute("scale", scale);  
        request.getRequestDispatcher("index.jsp").forward(request, response);  
    }  
}  

Step 5: Create the Frontend

1. Configure web.xml

<web-app>  
    <servlet>  
        <servlet-name>ConverterServlet</servlet-name>  
        <servlet-class>com.example.temperature.ConverterServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>ConverterServlet</servlet-name>  
        <url-pattern>/convert</url-pattern>  
    </servlet-mapping>  
</web-app>  

2. Design index.jsp

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>Temperature Converter</title>  
</head>  
<body>  
    <h1>Temperature Converter</h1>  
    <form action="convert" method="post">  
        <label for="temperature">Enter Temperature:</label>  
        <input type="number" id="temperature" name="temperature" required>  

        <label for="scale">Convert To:</label>  
        <select id="scale" name="scale">  
            <option value="Celsius">Celsius</option>  
            <option value="Fahrenheit">Fahrenheit</option>  
            <option value="Kelvin">Kelvin</option>  
        </select>  

        <button type="submit">Convert</button>  
    </form>  

    <c:if test="${not empty result}">  
        <h2>Converted Temperature: ${result} ${scale}</h2>  
    </c:if>  
</body>  
</html>  

Step 6: Deploy and Run

  1. Package the project into a WAR file:
mvn clean package  
  1. Deploy the WAR file to your Tomcat server.
  2. Start the server and open your browser at http://localhost:8080/temperature-converter.

Step 7: Test the Application

Enter a temperature value, select the target scale (Celsius, Fahrenheit, or Kelvin), and click “Convert.” The converted value will be displayed on the page.

 

Post Comment