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

TYBBA java servlet

This document outlines the steps to run a Java Servlet with JDBC in Eclipse using Apache Tomcat 10.1. It includes instructions for setting up the server, creating a dynamic web project, and coding a servlet that connects to a MySQL database to insert and display student records. Finally, it describes how to run the project on a server and test it through a web browser.

Uploaded by

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

TYBBA java servlet

This document outlines the steps to run a Java Servlet with JDBC in Eclipse using Apache Tomcat 10.1. It includes instructions for setting up the server, creating a dynamic web project, and coding a servlet that connects to a MySQL database to insert and display student records. Finally, it describes how to run the project on a server and test it through a web browser.

Uploaded by

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

TYBBA (CA)

Steps to run JAVA Servlet + jdbc Programs

1. In Eclipse :

Window -> Preferences -> Server ->Runtime Environment


Add
Select Apache Tomcat 10.1
Select the folder where Tomcat is stored
Apply

2. Create New Project


Web -> Dynamic Web Project
Select Generate Web.xml development Descriptor

3. Create Home Page html file

src->webapp->new html file

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="MyForm" action="MyClass">
<input type="submit">
</form>
</body>
</html>

4. Java Resources -> Create New Package


5. In new Package create new Servlet
Servlet Name = MyClass

6. Add mysql / postgresql driver .jar file

Project -> Properties -> Java Build Path -> Libraries


Add External JAR
Select .jar file
Apply
1
Page
7. Type following Code

package Mypackage;
import java.io.*;
import java.io.PrintWriter;
import java.sql.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

/**
* Servlet implementation class MyClass
*/
public class MyClass extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
private static final String URL =
"jdbc:mysql://localhost:3306/school";
private static final String USER = "root";
private static final String PASSWORD = "pmd123";

public MyClass() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());

response.setContentType("text/html");
PrintWriter out = response.getWriter();

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(URL,
USER, PASSWORD);
2

String query="insert into student values(?,?)";


Page
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setInt(1, 1);
stmt.setString(2, "Ram");

stmt.executeUpdate();

out.println("Connected");
query="SELECT * FROM student";
Statement st=conn.createStatement();
ResultSet rs = st.executeQuery(query);

out.println("<h2>Database Records:</h2>");
while (rs.next()) {
out.println("<p>Roll: " + rs.getInt("Roll") + ",
Name: " + rs.getString("Name") + "</p>");
}

rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
out.println("<h3>Error: " + e.getMessage() + "</h3>");
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
doGet(request, response);
}

8. Save and Click on Project Name

Run -> Run As -> Run on Server

Webpage will open in Chrome -> Click on submit


3
Page

You might also like