Java servlet view example

Java servlet are portable because you can create a servlet and it will run on any flat form. For example if you create your servlet in Windows environment you can run it on other operating system like Linux. The Java servlet below is an example code that select data stored in the MySQL database and display it in web browser so that it can be viewed by user. You can view data stored in database using the select SQL statement.

To view record you need first to create an interface so that you can display the data to the user in presentable manner. You need to create a code using Java that will access to database and you need to format the output using HTML.

Before creating a Java code that can access data and view it on browser, you need to create a database in MySQL name it as “registration” and create a table name it as “student” with following fields: id(auto_increment), fname, mi, lname, birthday, address, sex, civilstatus.

Java code:

package registration;
import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Viewregistration extends HttpServlet {
  public void service(HttpServletRequest request,
  HttpServletResponse response)
  throws IOException, ServletException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<html>");
  out.println("<head><title>View Records</title></head>");
  out.println("<body>");
  out.println("<a href='http://localhost:8081/registration/'>[Home]</a>");
  out.println("<h1>List of Registered Applicants</h1>");
  // connecting to database
  Connection con = null;  
  Statement stmt = null;
  ResultSet rs = null;
  try {
  Class.forName("com.mysql.jdbc.Driver");
  con =DriverManager.getConnection 
  ("jdbc:mysql://localhost:3306/registration","root","");
  stmt = con.createStatement();
  rs = stmt.executeQuery("SELECT * FROM student");
  // displaying records

 out.print("<table border='1'>");
 out.print("<tr'>");
 out.print("<td>");
 out.print("<strong><font size='2'>ID</font></strong>");
 out.print("</td>");

 out.print("<td>");
 out.print("<strong><font size='2'>First Name</font></strong>");
 out.print("</td>");

 out.print("<td>");
 out.print("<strong><font size='2'>M.I.</font></strong>");
 out.print("</td>");

 out.print("<td>");
 out.print("<strong><font size='2'>Last Name</font></strong>");
 out.print("</td>");

 out.print("<td>");
 out.print("<strong><font size='2'>Birth Day</font></strong>");
 out.print("</td>");

 out.print("<td>");
 out.print("<strong><font size='2'>Address</font></strong>");
 out.print("</td>");

 out.print("<td>");
 out.print("<strong><font size='2'>Sex</font></strong>");
 out.print("</td>");

out.print("<td>");
 out.print("<strong><font size='2'>Civil Status</font></strong>");
 out.print("</td>");
 out.print("</tr>");

while(rs.next()){
  out.print("<tr>");
  out.print("<td>");
  out.print(rs.getObject(1).toString());
  out.print("</td>");
  out.print("<td>");
  out.print(rs.getObject(2).toString());
  out.print("</td>");
  out.print("<td>");
  out.print(rs.getObject(3).toString());
  out.print("</td>");
  out.print("<td>");
  out.print(rs.getObject(4).toString());
  out.print("</td>");
  out.print("<td>");
  out.print(rs.getObject(5).toString());
  out.print("</td>");
  out.print("<td>");
  out.print(rs.getObject(6).toString());
  out.print("</td>");
  out.print("<td>");
  out.print(rs.getObject(7).toString());
  out.print("</td>");
  out.print("<td>");
  out.print(rs.getObject(8).toString());
  out.print("</td>");
 }
out.print("</table>");
out.println("</body></html>"); 
  } catch (SQLException e) {
 throw new ServletException("Servlet Could not display records.", e);
  } catch (ClassNotFoundException e) {
  throw new ServletException("JDBC Driver not found.", e);
  } finally {
  try {
  if(rs != null) {
  rs.close();
  rs = null;
  }
  if(stmt != null) {
  stmt.close();
  stmt = null;
  }
  if(con != null) {
  con.close();
  con = null;
  }
  } catch (SQLException e) {}
  }
  out.close();
  }
  }

The code above is a servlet that connect to MySQL database, select the records in database table and display the result in web browser.