package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.logic.AddPetCommand;
import beans.logic.DelOwnerCommand;
import beans.logic.DelPetCommand;
import beans.logic.DelRecordCommand;
import beans.logic.DelVetCommand;
import beans.logic.EditVetCommand;
import beans.logic.FindPasswordCommand;
import beans.logic.ICommand;
import beans.logic.LoginCommand;
import beans.logic.NullCommand;
import beans.logic.PageCommand;
import beans.logic.RegisterCommand;
import beans.logic.SavaPetCommand;
import beans.logic.SaveOwnerCommand;
import beans.logic.SavePetRecordCommand;
import beans.logic.SaveUpdateOwnerCommand;
import beans.logic.SaveUpdatePetCommand;
import beans.logic.SaveUpdatePetRecordCommand;
import beans.logic.SaveUpdateVetCommand;
import beans.logic.SaveVetCommand;
import beans.logic.ShowEditOwnerCommand;
import beans.logic.ShowEditPetCommand;
import beans.logic.ShowEditPetRecordCommand;
import beans.logic.ShowOwnCommand;
import beans.logic.ShowPetCommand;
import beans.logic.ShowPetRecordCommand;
import beans.logic.ShowVetCommand;
public class Controller extends HttpServlet
{
private ServletContext context;
public void init(ServletConfig config) throws ServletException
{
//初始化时创建逻辑类实例,存入ServletContext
super.init(config);
context=config.getServletContext();
context.setAttribute("loginCmd", new LoginCommand("/WEB-INF/pages/main.jsp"));
context.setAttribute("addPet", new AddPetCommand("/WEB-INF/pages/addPet.jsp"));
//查找宠物信息的业务逻辑
context.setAttribute("showPet", new ShowPetCommand("/WEB-INF/pages/showPet.jsp"));
//查找兽医信息的业务逻辑
context.setAttribute("showVet", new ShowVetCommand("/WEB-INF/pages/showVet.jsp"));
//查找主人信息的业务逻辑
context.setAttribute("showOwn", new AddPetCommand("/WEB-INF/pages/showOwn.jsp"));
//保存宠物信息的业务逻辑
context.setAttribute("savePet", new SavaPetCommand("/WEB-INF/pages/savePet.jsp"));
//分页转发
context.setAttribute("page", new PageCommand());
//转向修改页面的业务逻辑
context.setAttribute("updatePet", new ShowPetCommand("/WEB-INF/pages/updatePet.jsp"));
context.setAttribute("editPet", new ShowEditPetCommand("/WEB-INF/pages/editPet.jsp"));
//转向兽医添加的页面
context.setAttribute("addVet", new NullCommand("/WEB-INF/pages/addVet.jsp"));
//保存兽医信息成功后转向显示信息页面的业务逻辑
context.setAttribute("saveVet", new SaveVetCommand("/WEB-INF/pages/saveVet.jsp"));
context.setAttribute("updateVet", new ShowVetCommand("/WEB-INF/pages/updateVet.jsp"));
context.setAttribute("editVet", new EditVetCommand("/WEB-INF/pages/editVet.jsp"));
//转向添加主人信息页面的业务逻辑
context.setAttribute("addOwner", new NullCommand("/WEB-INF/pages/addOwner.jsp"));
//保存宠物主人信息的业务逻辑
context.setAttribute("saveOwner", new SaveOwnerCommand("/WEB-INF/pages/saveOwner.jsp"));
context.setAttribute("updateOwn", new ShowOwnCommand("/WEB-INF/pages/updateOwn.jsp"));
context.setAttribute("editOwn", new ShowEditOwnerCommand("/WEB-INF/pages/editOwner.jsp"));
//转向宠物病历新增的页面的业务
context.setAttribute("addPetRecord", new NullCommand("/WEB-INF/pages/addPetRecord.jsp"));
//保存添加的宠物的病历的业务逻辑
context.setAttribute("savePetRecord", new SavePetRecordCommand("/WEB-INF/pages/savePetRecord.jsp"));
//查找病历的业务逻辑
context.setAttribute("showPetRecord", new ShowPetRecordCommand("/WEB-INF/pages/showPetRecord.jsp"));
//转向的业务逻辑
context.setAttribute("updatePetRecord", new NullCommand("/WEB-INF/pages/updatePetRecord.jsp"));
context.setAttribute("editPetRecord", new ShowEditPetRecordCommand("/WEB-INF/pages/editPetRecord.jsp"));
//修改宠物信息成功的业务逻辑
context.setAttribute("saveUpdatePet", new SaveUpdatePetCommand("/WEB-INF/pages/saveUpdateSuess.jsp"));
//修改兽医信息成功的业务逻辑
context.setAttribute("saveUpdateVet", new SaveUpdateVetCommand("/WEB-INF/pages/saveUpdateSuess.jsp"));
//修改主人信息成功的业务逻辑
context.setAttribute("saveUpdateOwner", new SaveUpdateOwnerCommand("/WEB-INF/pages/saveUpdateSuess.jsp"));
//修改宠物病历信息成功的业务逻辑
context.setAttribute("saveUpdatePetRecord", new SaveUpdatePetRecordCommand("/WEB-INF/pages/saveUpdateSuess.jsp"));
context.setAttribute("delPet", new NullCommand("/WEB-INF/pages/showDelPet.jsp"));
//删除宠物信息的业务逻辑
context.setAttribute("delPetSuess", new DelPetCommand("/WEB-INF/pages/delSuess.jsp"));
context.setAttribute("delVet", new NullCommand("/WEB-INF/pages/showDelVet.jsp"));
context.setAttribute("delVetSuess", new DelVetCommand("/WEB-INF/pages/delSuess.jsp"));
context.setAttribute("delOwner", new NullCommand("/WEB-INF/pages/delOwner.jsp"));
context.setAttribute("delOwnerSuess", new DelOwnerCommand("/WEB-INF/pages/delSuess.jsp"));
context.setAttribute("delPetRecord",new NullCommand("/WEB-INF/pages/showDelRecord.jsp"));
context.setAttribute("delSuess", new DelRecordCommand("/WEB-INF/pages/delSuess.jsp"));
//转向注册页面
context.setAttribute("showRegister", new NullCommand("/WEB-INF/pages/register.jsp"));
context.setAttribute("saveRegister", new RegisterCommand("/WEB-INF/pages/saveRegister.jsp"));
context.setAttribute("home", new NullCommand("/WEB-INF/pages/main.jsp"));
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String cmd=request.getParameter("cmd");
ICommand command=(ICommand)context.getAttribute(cmd);
String nextPage=command.execute(request);
request.getRequestDispatcher(nextPage).forward(request, response);
}
@Override
public void destroy()
{
//把逻辑实例移除
context.removeAttribute("loginCmd");
super.destroy();
}
}
评论0
最新资源