package jsp.cotroller;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jsp.impl.Studentimpl;
import jsp.model.Student;
public class StudentSer extends HttpServlet {
Studentimpl studentimpl = new Studentimpl();
/**
* Constructor of the object.
*/
public StudentSer() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* 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
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String status = request.getParameter("status");
if(status.equals("saveStudent")){
Student student = new Student();
String name = request.getParameter("sname");
String address = request.getParameter("saddress");
String[] hobby = request.getParameterValues("shobby");
String str = studentimpl.subString(hobby);
student.setName(name);
student.setAddress(address);
student.setHobby(str);
studentimpl.savaStudent(student);
}else if(status.equals("queryStudent")){
String sname = request.getParameter("sname");
request.getSession().setAttribute("sname", sname);
ArrayList<Student> students = studentimpl.queryStudent(sname);
request.setAttribute("studentparam", students);
RequestDispatcher dispatcher = request.getRequestDispatcher("/queryStudent.jsp");
dispatcher.forward(request, response);
}else if(status.equals("deleteStudent")){
int sid = Integer.parseInt(request.getParameter("sid"));
studentimpl.deleteStudent(sid);
String sname = request.getSession().getAttribute("sname").toString();
ArrayList<Student> students = studentimpl.queryStudent(sname);
request.setAttribute("studentparam", students);
RequestDispatcher dispatcher = request.getRequestDispatcher("/queryStudent.jsp");
dispatcher.forward(request, response);
//RequestDispatcher dispatcher = request.getRequestDispatcher("/queryStudent.jsp");
//dispatcher.forward(request, response);
}else if(status.equals("updateStudent")){
Student student = new Student();
String sid = request.getParameter("sid");
String name = request.getParameter("sname");
String address = request.getParameter("saddress");
String[] hobby = request.getParameterValues("shobby");
String str = studentimpl.subString(hobby);
student.setId(sid);
student.setName(name);
student.setAddress(address);
student.setHobby(str);
studentimpl.updateStudent(student);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}