📄 controlservlet.java
字号:
/*
控制器
作用:
在初始化时实例“模型”,并放入上下文(保证模型有且仅有一个实例)
在收到请求时取出参数,调用模型实例完成操作,并把数据状态存放在Request中
转交数据状态到下一个视图
在自己毁灭之前,销毁资源
*/
package TestClass;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ControlServlet extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
config.getServletContext().setAttribute("toMain",new LoginCommand("/Main.jsp"));
config.getServletContext().setAttribute("toTest",new StartCommand("/Test.jsp"));
config.getServletContext().setAttribute("SetScore",new ScoreCommand("/End.htm"));
config.getServletContext().setAttribute("BackTest",new NullCommand("/Login.jsp"));
config.getServletContext().setAttribute("GetScore",new ScoreListCommand("/Score.jsp"));
config.getServletContext().setAttribute("ReturnTest",new NullCommand("/Manager.htm"));
config.getServletContext().setAttribute("PrintXML",new PrintCommand("/Score.jsp"));
super.init(config);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//取出传来的命令参数信息
String cmd = request.getParameter("cmd");
//从上下文中取出业务操作组件实例
Command cmdObj=(Command)this.getServletContext().getAttribute(cmd);
//调用模型方法,并将返回值放在request中
String next=cmdObj.execute(request,response);
//重定向页面,指定视图
this.getServletContext().getRequestDispatcher(next).forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
public void destroy()
{
this.getServletContext().removeAttribute("toMain");
this.getServletContext().removeAttribute("toTest");
this.getServletContext().removeAttribute("SetScore");
this.getServletContext().removeAttribute("BackTest");
this.getServletContext().removeAttribute("GetScore");
this.getServletContext().removeAttribute("ReturnTest");
this.getServletContext().removeAttribute("PrintXML");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -