package com.michael.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.michael.dao.StudentDao;
import com.michael.dao.impl.StudentDaoImpl;
import com.michael.vo.Student;
public class StuServlet
extends HttpServlet {
/** * Constructor of the object. */ public StuServlet() {
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 {
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 {
String methodName = request.getParameter(
"methodName");
if(methodName!=
null&&methodName.equals(
"add")){
add(request,response);
}
else{
query(request,response);
}
/* //响应用户请求 String name = request.getParameter("name"); String age = request.getParameter("age"); String email = request.getParameter("email"); //调用后台逻辑 StudentDao dao = new StudentDaoImpl(); Student stu = new Student(); stu.setName(name); stu.setAge(new Integer(age)); stu.setEmail(email); dao.add(stu); List list = dao.listStudent(); request.setAttribute("StuList", list); //数据处理后跳转 request.getRequestDispatcher("/stu.jsp").forward(request,response); */ }
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应用户请求 String name = request.getParameter(
"name");
String age = request.getParameter(
"age");
String email = request.getParameter(
"email");
//调用后台逻辑 StudentDao dao =
new StudentDaoImpl();
Student stu =
new Student();
stu.setName(name);
stu.setAge(
new Integer(age));
stu.setEmail(email);
dao.add(stu);
query(request,response);
}
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用后台逻辑 StudentDao dao =
new StudentDaoImpl();
List list = dao.listStudent();
request.setAttribute(
"StuList", list);
// 跳转 request.getRequestDispatcher(
"/stu.jsp").forward(request, response);
}
/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init()
throws ServletException {
// Put your code here }
}