2.2 使用模型视图控制器(MVC)体系结构风格设计模式
如图2 所示,集成使用了servlets 和JSP 页面。在该模型中,JSP 页面用于
表示层,并且servlets负责处理各类任务。Servlet 作为一个控制器,负责处理请
求并创建JSP 页面所需的任何bean。该控制器也负责确定将该请求传递到哪个
JSP页面。JSP页面检索servlet 创建的对象,并提取动态内容插入在一个模板中。
作为控制器Servlet 的结构如下:
public class ControllerServlet extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private Map handlerHash = new HashMap();
public static String DataURL = null;
public static String ForumMarkURL = null;
public static String PastePicsURL = null;
private String getServletParam(String paramName)
{
String value = this.getServletConfig().getInitParameter(paramName);
if (value==null)
return "";本文来自辣^文*论^文~网
else
return value;
}//getServletParam
//Initialize global variables
public void init() throws ServletException
{
//read config values from the web.xml
DataURL = getServletParam("DataURL").trim();
ForumMarkURL = PastePicsURL = this.getServletContext().getRealPath("/");
ForumMarkURL += "images/forummark/";
PastePicsURL += "images/pastepics/";
// ForumMarkURL += "images\\forummark\\";
// PastePicsURL += "images\\pastepics\\";
handlerHash.put("/index.html",new Index());
handlerHash.put("/luntanleft.html",new LuntanLeft());
handlerHash.put("/manageforumtype.html",new ManageForumType());
handlerHash.put("/newforum.html",new NewForum());
handlerHash.put("/manageforumlist.html",new ManageForumList());
handlerHash.put("/editforum.html",new EditForum());
handlerHash.put("/forumlist.html",new ForumList());
handlerHash.put("/bulletinlist.html",new BulletinList());
handlerHash.put("/newbulletin.html",new NewBulletin());
handlerHash.put("/uploadpastepic.html",new UploadPastePic());
handlerHash.put("/addtoceiling.html",new AddToCeiling());
handlerHash.put("/addtoessential.html",new AddToEssential());
handlerHash.put("/deletebulletin.html",new DeleteBulletin());
handlerHash.put("/displaybulletin.html",new DisplayBulletin());
handlerHash.put("/ansbulletin.html",new AnsBulletin());
handlerHash.put("/editbulletin.html",new EditBulletin());
handlerHash.put("/uploadforummark.html",new UploadForumMark());
handlerHash.put("/favoritebulletin.html",new FavoriteBulletin());
handlerHash.put("/queryresult.html",new QueryResult());
handlerHash.put("/passwordmanage.html",new PasswordManage());
handlerHash.put("/newuser.html",new NewUser());
handlerHash.put("/manageuserlist.html",new ManageUserList());
handlerHash.put("/listessential.html",new ListEssential());
handlerHash.put("/apcontrol.html",new APControl());
handlerHash.put("/announcement.html",new Announcement());
handlerHash.put("/newannounce.html",new NewAnnounce());
handlerHash.put("/editannounce.html",new EditAnnounce());
handlerHash.put("/deleteannounce.html",new DeleteAnnounce());
handlerHash.put("/editannouncecontent.html",new EditAnnounceContent());
handlerHash.put("/manageuserinfo.html",new ManageUserInfo());
}//init
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
RequestHandler rh =
(RequestHandler)handlerHash.get(request.getServletPath());
if(rh == null)
{本文来自辣^文*论^文~网
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
else
{
String viewURL = rh.handleRequest(request,response);
毕业论文http://www.751com.cn
}//doGet
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request,response);
}//doPost
}
可以看出,在这里ControllerServlet 的主要功能是文护一个”请求处理器”的
列表,然后根据受到的Http 请求,找到合适的请求处理器,把该Http 请求转到
请求处理器处理。
请求处理器处理根据收到的Http 请求内容,改变Application 或者Session
的状态,进行数据库操作等等;并且返回用于显示给用户浏览的JSP 的URL 给
ControllerServlet,由它控制页面跳转。
每一个不包括参数的不同的URL 都对应不同的”请求处理器”,所有的请求
处理器都有实现一个简单的接口RequestHandler。
public interface RequestHandler
{
String handleRequest(HttpServletRequest request,HttpServletResponse
response) throws javax.servlet.ServletException,IOException;
}//END interface
例如:
发表帖子的Http 请求"/newbulletin.html"由请求处理器NewBulletin 进行处