import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
* This Servlet prints all registered students.
*
*
* ErrorDisplayerServlet Servlet to handle them.
*
* @author Sofoklis Stouraitis
*/
public class ViewRegisteredStudents extends HttpServlet {
/**
* Handles HTTP GET requests.
*
* @param request
* the request object
* @param response
* the response object
*
* @throws IOException
* if an input or output error is detected when the servlet
* handles the POST request.
* @throws ServletException
* if the request for the POST could not be handled.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html; charset=ISO-8859-7");
PrintWriter out = new PrintWriter(response.getWriter(), true);
ResultSet rs1 = null;
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/servlet/ErrorDisplayerServlet");
try {
/*
* Initialize StudentClass Object.
*/
StudentClass student= new StudentClass();
/*
* Establish connection with database.
*/
student.open();
/*
* Executes query and gets the results.
*/
rs1 = student.getAllStudents();
if (rs1 == null) {
student.close();
/*
* Puts the error message to the request object.
*/
request.setAttribute("error", student.getErrorMessages());
/*
* Forwards the request to the ErrorDisplayerServlet to print
* it.
*/
dispatcher.forward(request, response);
return;
}
/*
* Moves the cursor down one row in order to check if there is a record in the database
*/
if(!rs1.next()) {
request.setAttribute("error", "Δεν έχει γίνει κάποια καταχώρηση!");
dispatcher.forward(request, response);
return;
}
/*
* Moves the cursor to the previous row (to the start) in order to print all records
*/
rs1.previous();
out.println("<html>");
out.println("<head>");
out.println("<Meta Http-Equiv='Content-Type' Content='text/html; Charset=iso-8859-7'>");
out.println("<title>Προσωπικό Εκπαιδευτικού Εργαστηρίου Πληροφορικής</title>");
out.println("</head>");
out.println("<body>");
out.println("<br><div align='center'>");
out.println("<h2>Έχουν καταχωρηθεί οι παρακάτω φοιτητές:</h2>");
out.println("<table border='1' width='60%'>");
out.println("<tr>");
out.println("<td width='11%' bgcolor='#C0C0C0'>");
out.println("<p align='center'><b><font color='#000080'>A.M :</font></b></td>");
out.println("<td width='47%' bgcolor='#C0C0C0'>");
out.println("<p align='center'><b><font color='#000080'>SURNAME :</font></b></td>");
out.println("<td width='42%' bgcolor='#C0C0C0'>");
out.println("<p align='center'><b><font color='#000080'>NAME :</font></b></td>");
out.println("</tr>");
while (rs1.next()) {
/*
* gets data from database.
*/
String am = rs1.getString("AM");
String surname = rs1.getString("Surname");
String name = rs1.getString("name");
out.println("<tr>");
out.println("<td width='11%' align='center'>" + am + "</td>");
out.println("<td width='39%' align='center'>" + surname + "</td>");
out.println("<td width='44%' align='center'>" + name + "</td>");
out.println("</tr>");
}
out.println("</table></div>");
out.println("</body>");
out.println("</html>");
/*
* ends the connection with database
*/
student.close();
} catch (Exception e) {
/*
* puts the error message to the request object.
*/
request.setAttribute("error", e.getMessage());
/*
* Forwards the request to the ErrorDisplayerServlet to print it.
*/
dispatcher.forward(request, response);
}
}
}//end of class
|