0% found this document useful (0 votes)
23 views5 pages

Steps To Set Path For Tomcat

This document provides a step-by-step guide to set up Apache Tomcat in Eclipse for Java server programming. It includes instructions for downloading Tomcat, configuring it in Eclipse, creating a dynamic web project, and developing a simple servlet program. Additionally, it offers optional web.xml configuration and instructions to run the servlet on Tomcat, along with two HTML examples for user interaction.

Uploaded by

dali cocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Steps To Set Path For Tomcat

This document provides a step-by-step guide to set up Apache Tomcat in Eclipse for Java server programming. It includes instructions for downloading Tomcat, configuring it in Eclipse, creating a dynamic web project, and developing a simple servlet program. Additionally, it offers optional web.xml configuration and instructions to run the servlet on Tomcat, along with two HTML examples for user interaction.

Uploaded by

dali cocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Steps to Set Up Apache Tomcat in Eclipse for Java Server Programming

Follow these steps to configure Apache Tomcat in Eclipse IDE and run a simple Java Servlet
program.

Step 1: Download and Install Apache Tomcat

1. Download Apache Tomcat from the official website:


👉 https://tomcat.apache.org/download-90.cgi
2. Extract the ZIP file to a directory, e.g., C:\apache-tomcat-9.0.XX (Windows) or
/opt/tomcat (Linux/Mac).

Step 2: Configure Tomcat in Eclipse

1. Open Eclipse IDE (Ensure you have Eclipse IDE for Java EE Developers).
2. Click on Window → Preferences.
3. Navigate to Server → Runtime Environments.
4. Click Add → Choose Apache Tomcat v9.0 (or your version).
5. Click Next, then browse and select the Tomcat installation folder (C:\apache-tomcat-
9.0.XX).
6. Click Finish and then Apply and Close.

Step 3: Create a Dynamic Web Project

1. In Eclipse, go to File → New → Dynamic Web Project.


2. Enter the project name (e.g., MyServletApp).
3. Select Target Runtime → Choose Apache Tomcat v9.0.
4. Click Next → Finish.
Step 4: Create a Simple Servlet Program

1. Right-click on src/main/java → New → Servlet.


2. Enter Servlet Name (e.g., HelloServlet).
3. Package Name: com.example → Click Finish.
4. Modify the HelloServlet.java file:

import java.io.IOException;
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("/hello")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("<h2>Hello, Welcome to Java Servlet
Programming!</h2>");
}
}

Step 5: Configure web.xml (Optional)

If not using @WebServlet, update web.xml (inside WEB-INF/):

xml
CopyEdit
<web-app>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

Step 6: Run the Servlet on Tomcat

1. Right-click on the project → Run As → Run on Server.


2. Select Apache Tomcat v9.0 → Click Finish.
3. Open a browser and go to:

http://localhost:8080/MyServletApp/hello

4. You should see:

css
Hello, Welcome to Java Servlet Programming!

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////

1ST HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
HELLO STUDENTS
</body>
</html>

2ND HTML

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Sum of Two Numbers</title>

<style>

body {

font-family: Arial, sans-serif;

text-align: center;
margin-top: 50px;

form {

display: inline-block;

padding: 20px;

border: 1px solid #ccc;

border-radius: 10px;

background-color: #f9f9f9;

input, button {

margin: 10px;

padding: 8px;

font-size: 16px;

</style>

</head>

<body>

<h2>Enter Two Numbers</h2>

<form onsubmit="calculateSum(event)">

<label>Number 1:</label>

<input type="number" id="num1" required><br>

<label>Number 2:</label>

<input type="number" id="num2" required><br>

<button type="submit">Calculate Sum</button>

</form>
<h3 id="result"></h3>

<script>

function calculateSum(event) {

event.preventDefault(); // Prevents page reload

let num1 = parseFloat(document.getElementById("num1").value);

let num2 = parseFloat(document.getElementById("num2").value);

let sum = num1 + num2;

document.getElementById("result").innerHTML = "Sum: " + sum;

</script>

</body>

</html>

You might also like