package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.PetService;
import service.impl.PetServiceImpl;
import entity.pet;
public class PetServlet extends HttpServlet {
/**
* 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 {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
PetService service = new PetServiceImpl();
String opr = request.getParameter("opr");
if ("all".equals(opr)) {
List<pet> list = new ArrayList<pet>();
list = service.findAllPets();
request.setAttribute("list", list);
request.getRequestDispatcher("index.jsp")
.forward(request, response);
} else if ("breed".equals(opr)) {
List<pet> list = new ArrayList<pet>();
String breed = request.getParameter("pbreed");
if (breed == "" || breed == null) {
response.sendRedirect("petServlet?opr=all");
}
list = service.findPetsByBreed(breed);
request.setAttribute("list", list);
request.getRequestDispatcher("index.jsp")
.forward(request, response);
} else if ("add".equals(opr)) {
String pet_name = request.getParameter("pet_name");
String pet_breed = request.getParameter("pet_breed");
String pet_sex = request.getParameter("pet_sex");
String brithday = request.getParameter("brithday");
String description = request.getParameter("description");
boolean flag = true;
if (pet_name == "" || pet_name == null) {
flag = false;
}
if (pet_breed == "" || pet_breed == null) {
flag = false;
}
if (pet_sex == "" || pet_sex == null) {
flag = false;
}
if (brithday == "" || brithday == null) {
flag = false;
}
if (flag == false) {
response.sendRedirect("pet_add.jsp");
} else {
pet p = new pet();
p.setPet_name(pet_name);
p.setPet_breed(pet_breed);
p.setPet_sex(pet_sex);
try {
p.setBrithday(new SimpleDateFormat("yyyy-MM-dd")
.parse(brithday));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.setDescription(description);
if (service.addPet(p)) {
out.print("<script type='text/javascript'>");
out.print("alert('更新成功!');");
out.print("window.location.href='petServlet?opr=all';");
out.print("</script>");
}else{
out.print("<script type='text/javascript'>");
out.print("alert('更新失败!');");
out.print("window.location.href='pet_add.jsp';");
out.print("</script>");
}
}
}
out.flush();
out.close();
}
}