import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
* This Servlet prints all registered students. If encounter an error, it
* forwards the error to the ErrorDisplayerServlet Servlet to handle them.
*
* @author Sofoklis Stouraitis
*/
public class ViewRegisteredStudentsUpdated 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 StudentClassB Object.
*/
StudentClassB student= new StudentClassB();
/*
* Establish connection with database.
*/
student.open();
/*
* gets the parameter from the request.
*/
String operation = request.getParameter("userChoise");
/*
* Determines who called the servlet.
* If the Servlet (ViewRegisteredStudentsUpdated) was called by itself (through the form 'callMyself')
* the String 'operation' will be equal to: 'NO' or 'AM' or 'surname' or 'Name' or 'IP' or 'Regdate'.
* But, if the the Servlet was called directly from a web browser or through the page 'exercise9.htm' it will have 'null' value.
* This will lead to a NullPointerException at 'StudentClassB' class, so we need a trick to avoid the Exception.
* In other words, at this point we 'say' to the Servlet the following:
* <<If someone call you directly, then bring the results without any sort criteria>>
*/
if(operation == null)
operation = "NO";
/*
* Executes query and gets the results.
*/
rs1 = student.getAllStudents(operation);
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 bgcolor='#FFCCFF'>");
out.println("<br><div align='center'>");
out.println("<h2>Έχουν καταχωρηθεί οι παρακάτω φοιτητές:</h2>");
if(operation.equals("NO"))
out.println("<h3>Χωρίς ταξινόμηση</h3>");
else
out.println("<h3>Ταξινόμηση ανά " + operation + "</h3>");
out.println("<table border='1' width='90%'>");
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='28%' bgcolor='#C0C0C0'>");
out.println("<p align='center'><b><font color='#000080'>SURNAME :</font></b></td>");
out.println("<td width='28%' bgcolor='#C0C0C0'>");
out.println("<p align='center'><b><font color='#000080'>NAME :</font></b></td>");
out.println("<td width='15%' bgcolor='#C0C0C0'>");
out.println("<p align='center'><b><font color='#000080'>IP :</font></b></td>");
out.println("<td width='28%' bgcolor='#C0C0C0'>");
out.println("<p align='center'><b><font color='#000080'>Date :</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");
String ip = rs1.getString("IP");
String timestamp = String.valueOf(rs1.getTimestamp("RegDate"));
out.println("<tr>");
out.println("<td width='11%' align='center'>" + am + "</td>");
out.println("<td width='28%' align='center'>" + surname + "</td>");
out.println("<td width='28%' align='center'>" + name + "</td>");
out.println("<td width='15%' align='center'>" + ip + "</td>");
out.println("<td width='28%' align='center'>" + timestamp + "</td>");
out.println("</tr>");
}
out.println("</table>");
out.println(" <form name='callMyself' method='get' action='ViewRegisteredStudentsUpdated'>");
out.println(" <p> </p>");
out.println(" <table width='60%' border='0'>");
out.println(" <tr> ");
out.println(" <td width='61%'><div align='right'></div></td>");
out.println(" <td width='39%'> </td>");
out.println(" </tr>");
out.println(" <tr> ");
out.println(" <td><div align='right'>Ταξινόμηση καταχωρήσεων ανά:</div></td>");
out.println(" <td><div align='left'>");
out.println(" <select name='userChoise' id='userChoise'>");
out.println(" <option value='NO'>Χωρίς ταξινόμηση</option>");
out.println(" <option value='AM'>Αριθμό Μητρώου</option>");
out.println(" <option value='Surname'>Επώνυμο</option>");
out.println(" <option value='Name'>Όνομα</option>");
out.println(" <option value='IP'>Διεύθυνση IP</option>");
out.println(" <option value='RegDate'>Ημερομηνία</option>");
out.println(" </select>");
out.println(" </div></td>");
out.println(" </tr>");
out.println(" <tr> ");
out.println(" <td><div align='right'> </div></td>");
out.println(" <td><input type='submit' name='Submit' value='Submit'></td>");
out.println(" </tr>");
out.println(" </table>");
out.println(" <p> </p>");
out.println("</form>");
out.println("</div></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
|