import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This servlet receives students personal data from an http request and performs the following check:
* 1)if all the Student data have value
* 2)if the Student has already been registered.
* 3)if the AM of the student is valid.
* Finally, register (insert) Student to the database.
*
* @author Sofoklis Stouraitis
*/
public class RegisterStudentUpdated 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 GET request
* @throws ServletException
* if the request for the GET 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);
String sName = request.getParameter("name");
String sSurname = request.getParameter("surname");
String sAM = request.getParameter("am");
String visitorsIPAddress = request.getRemoteAddr();
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/servlet/ErrorDisplayerServlet");
try {
sName = new String(sName.getBytes("ISO-8859-1"), "ISO-8859-7");
sSurname = new String(sSurname.getBytes("ISO-8859-1"), "ISO-8859-7");
sAM = new String(sAM.getBytes("ISO-8859-1"), "ISO-8859-7");
if (!(sName.length() > 0) || !(sSurname.length() > 0)
|| !(sAM.length() == 7)) {
request.setAttribute("error", "Δεν έχετε συμπληρώσει σωστά όλα τα στοιχεία σας!!!");
dispatcher.forward(request, response);
return;
}
/*
* checking students AM.
*/
checkAM(sAM);
/*
* Initialize StudentClassB Object.
*/
StudentClassB student = new StudentClassB();
/*
* Establish connection with database.
*/
student.open();
if(student.isStudentRegistered(sAM)) {
request.setAttribute("error", student.getErrorMessages());
student.close();
dispatcher.forward(request, response);
return;
}
student.registerStudent(sAM, sName, sSurname, visitorsIPAddress);
student.close();
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='#33FF99'>");
out.println("<h1>Καταχωρήσατε τα παρακάτω στοιχεία:</h1>");
out.println("<b>Όνομα: </b>" + sName + "<br>");
out.println("<b>Επώνυμο: </b>" + sSurname + "<br>");
out.println("<b>Αρ.Μητρώου: </b>" + sAM + "<br>");
out.println("<b>Διεύθυνση IP: </b>" + visitorsIPAddress + "<br>");
out.println("</body>");
out.println("</html>");
} catch (Exception ex) {
request.setAttribute("error", ex.getMessage());
dispatcher.forward(request, response);
out.println("</body>");
out.println("</html>");
}
}
/**
* Checks students am for validation
*
* @param am, Students AM
*
* @throws Exception
* (with the appropriate message) if any error occured or am is not valide.
*/
private void checkAM(String am) throws Exception {
try {
int amNum = Integer.parseInt(am);
String firstCheck = am.substring(0, 2);
if (!firstCheck.equals("80"))
throw new Exception("Ο '" + am + "' ΔΕΝ είναι εγκυρός Αριθμός Μητρώου, γιατί δεν ξεκινάει απο 80!");
if(am.charAt(3) != '0')
throw new Exception("Ο '" + am + "' ΔΕΝ είναι εγκυρός Αριθμός Μητρώου, γιατί το τέταρτο ψηφίο δεν ειναι μηδέν!");
int num = Integer.parseInt(am.substring(4, 7));
if(num > 190)
throw new Exception("Ο '" + am + "' ΔΕΝ είναι εγκυρός Αριθμός Μητρώου!");
} catch (NumberFormatException e) {
throw new Exception("Σφάλμα: Ο '" + am + "' ΔΕΝ είναι Αριθμός!");
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}// End of class
|