0% found this document useful (0 votes)
22 views

how to run servlet program using tomcat

program

Uploaded by

jananisb2207
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

how to run servlet program using tomcat

program

Uploaded by

jananisb2207
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Development Kit (JDK):

• Download and install JDK 11 or higher from Oracle or OpenJDK.


• Verify installation- java -version
1. Apache Tomcat Server:
o Download and install Tomcat 9 or higher from Tomcat's website.
o Unzip the downloaded file and set the CATALINA_HOME environment variable
to the installation directory.
2. MySQL Database:
o Download and install MySQL Server from MySQL Community Downloads.
o Install a GUI like MySQL Workbench or use the command-line client for
database management.
3. Eclipse IDE (optional but recommended):
o Download and install Eclipse IDE for Java EE Developers from Eclipse
Downloads.
4. JDBC Driver:
o Download the MySQL JDBC driver (mysql-connector-java.jar) from MySQL
Connector Downloads.
o Place the JAR file in the lib folder of Tomcat or your project's WEB-INF/lib
directory.

Step 1: Set Up MySQL Databases and Tables


Create contact_db and employee_db
1. Open MySQL Workbench or Command Line Client.
2. Run the following SQL commands:
CREATE DATABASE contact_db;

USE contact_db;

CREATE TABLE contacts (


id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
phone_number VARCHAR(15),
email VARCHAR(100) NOT NULL,
address TEXT,
date_of_birth DATE,
nickname VARCHAR(50)
);

-- Employee Database
CREATE DATABASE employee_db;

USE employee_db;
CREATE TABLE employees (
ecode VARCHAR(20) NOT NULL,
ename VARCHAR(50) NOT NULL,
desig VARCHAR(50),
dept VARCHAR(50),
basic DECIMAL(10, 2)
);
3. Verify the tables were created:
SHOW DATABASES;
USE contact_db;
SHOW TABLES;

Step 2: Configure the Project


Create a Dynamic Web Project
1. Open Eclipse IDE.
2. Click on File > New > Dynamic Web Project.
o Name the project: ContactEmployeeForm.
o Target Runtime: Select Apache Tomcat.
o Configuration: Select Dynamic Web Module Version 4.0.
3. Click Finish.
Add JDBC Driver
1. Right-click on the project > Build Path > Configure Build Path.
2. Add the mysql-connector-java.jar to the project:
o Go to Libraries > Classpath > Add External JARs.
o Select the downloaded MySQL Connector JAR.
Create Necessary Folders
1. Create the following folders in the project:
o WebContent (default).
o WEB-INF under WebContent.
o classes under WEB-INF.

Step 3: Add the Files


Add HTML Files
1. Place contact_form.html and employee_form.html inside WebContent.
Add Servlet Files
1. Create a new package in src (e.g., com.example.servlets).
2. Add ContactServlet.java and EmployeeServlet.java.

Step 4: Configure web.xml


1. Inside WEB-INF, create a file named web.xml.
2. Add the following content:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0">
<servlet>
<servlet-name>ContactServlet</servlet-name>
<servlet-class>com.example.servlets.ContactServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContactServlet</servlet-name>
<url-pattern>/ContactServlet</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>EmployeeServlet</servlet-name>
<servlet-class>com.example.servlets.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeServlet</servlet-name>
<url-pattern>/EmployeeServlet</url-pattern>
</servlet-mapping>
</web-app>

Step 5: Deploy and Run


Deploy to Tomcat
1. Right-click on the project > Run As > Run on Server.
2. Select Tomcat server.
Access the Web Pages
1. Open a browser and visit:
o For contact_form.html:
http://localhost:8080/ContactEmployeeForm/contact_form.html
o For employee_form.html:
http://localhost:8080/ContactEmployeeForm/employee_form.html
Submit Data
1. Fill out the forms and click submit.
2. Check:
o Database for inserted records (MySQL).
o XML file for saved employee data.

You might also like