0% found this document useful (0 votes)
8 views2 pages

Servlet Table Tutorial Guide Fixed

This teaching guide outlines how to display HTML tables using Java Servlets. It includes sample code for generating a static HTML table, as well as dynamic tables using data from an ArrayList and a MySQL database via JDBC. The document provides step-by-step instructions for connecting to a database and querying data to populate the table.

Uploaded by

ေမဇင္
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)
8 views2 pages

Servlet Table Tutorial Guide Fixed

This teaching guide outlines how to display HTML tables using Java Servlets. It includes sample code for generating a static HTML table, as well as dynamic tables using data from an ArrayList and a MySQL database via JDBC. The document provides step-by-step instructions for connecting to a database and querying data to populate the table.

Uploaded by

ေမဇင္
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/ 2

Java EE Servlet Table Output – Teaching Guide

1. Objective:
In this lesson, students will learn how to display HTML tables from a Java Servlet.

2. Output HTML Table from Servlet:


Sample code:
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html><body>");
[Link]("<h2>Student Table</h2>");
[Link]("<table border='1' cellpadding='10'>");
[Link]("<tr><th>ID</th><th>Name</th><th>Grade</th></tr>");
[Link]("<tr><td>1</td><td>Alice</td><td>A</td></tr>");
[Link]("<tr><td>2</td><td>Bob</td><td>B</td></tr>");
[Link]("<tr><td>3</td><td>Charlie</td><td>A+</td></tr>");
[Link]("</table>");
[Link]("</body></html>");

3. Table Data from Java ArrayList:


Sample:
class Student {
int id; String name; String grade;
Student(int id, String name, String grade) {
[Link] = id; [Link] = name; [Link] = grade;
}
}
List<Student> students = new ArrayList<>();
[Link](new Student(1, "Alice", "A"));
[Link](new Student(2, "Bob", "B+"));
[Link](new Student(3, "Charlie", "A-"));
for (Student s : students) {
[Link]("<tr><td>" + [Link] + "</td><td>" + [Link] + "</td><td>" + [Link] + "</td></tr>");
}

4. Table Data from MySQL Database (JDBC):


Steps:
1. Create MySQL table 'students'
2. Connect using JDBC
3. Query with SQL
4. Output rows

String jdbcURL = "jdbc:mysql://localhost:3306/yourdb";


String dbUser = "root";
String dbPass = "password";
Connection conn = [Link](jdbcURL, dbUser, dbPass);
String sql = "SELECT * FROM students";
Statement stmt = [Link]();
ResultSet rs = [Link](sql);
while ([Link]()) {
[Link]("<tr><td>" + [Link]("id") + "</td>");
[Link]("<td>" + [Link]("name") + "</td>");
[Link]("<td>" + [Link]("grade") + "</td></tr>");
}

You might also like