package controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Student;
import service.StudentService;

public class NewStudentController extends HttpServlet {

/**
* The doPost method of the servlet.

*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
private StudentService service = new StudentService();

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Student stu = new Student();
String pwd2 = request.getParameter("pwd2");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String pwd = request.getParameter("pwd");
String email = request.getParameter("email");
String phone = request.getParameter("phone");

stu.setName(name);
stu.setGender(gender);
stu.setPassword(pwd);
stu.setEmail(email);
stu.setPhone(phone);

try {
int id = service.newStudent(stu);
System.out.println("id is " + id);
} catch (SQLException e) {
e.printStackTrace();
request.setAttribute("error", e.getMessage());
request.getRequestDispatcher("register.jsp").forward(request, response);
return;
}
response.sendRedirect("index.jsp");

}

}