最近看看了PETSTORE2.0的一些代码,发现有很多东西值得学习。
下面我来分析一下ControllerServlet这个SERVLET,它位于com.sun.javaee.blueprints.petstore.controller包之中。它的作用是根据请求的SERVLET的URi完成不同的特定功能,我只看了产生随机图片的那个调用。
代码如下:
-
-
-
-
-
-
-
-
- public class ControllerServlet extends HttpServlet {
-
- private static final boolean bDebug=false;
- private HashMap<String, ControllerAction> actionMap = new HashMap<String, ControllerAction>();
-
- @Override
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- ServletContext context = config.getServletContext();
- CatalogFacade cf = (CatalogFacade) context.getAttribute("CatalogFacade");
- actionMap.put("/ImageServlet", new ImageAction(context));
- actionMap.put("/controller", new DefaultControllerAction(context));
- actionMap.put("/faces/CaptchaServlet", new CaptchaAction());
- actionMap.put("/TagServlet", new TagXmlAction(cf));
- actionMap.put("/catalog", new CatalogXmlAction(cf));
- }
-
- public ControllerAction findAction(String servletPath) {
- return actionMap.get(servletPath);
- }
- @Override
- public void destroy() {
- actionMap = null;
- }
-
- @Override
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String servletPath = request.getServletPath();
- if(bDebug) System.out.println(" ServletPath: " + servletPath + ", pathinfo: " + request.getPathInfo());
- ControllerAction action = actionMap.get(servletPath);
- if (action != null) {
- if(bDebug) System.out.println(" Found action " + action.getClass().getName());
- action.service(request, response);
- } else {
- PetstoreUtil.getLogger().log(Level.SEVERE, "Servlet '" + request.getServletPath() + "' not registered in ControllerServlet!!");
- HttpServletResponse httpResponse=(HttpServletResponse)response;
- httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
- }
- }
- }
它的运作流程可以很容易的看出来:首先SERVLET加载入所有需要处理的servlet URI,把他们都存入一个HASHmAP当中,然后在DOGET中,读取请求的SERVLET的RUI,得到特定的URI后就进行相应的操作。
分享到:
相关推荐
原生Servlet与Spring Controller性能比较 博客中的 Servlet项目源码
购物servlet行为分析,对购物车流程很清楚!适用于初学者!
Java WEB 篇八 tomcat 容器是如何创建 servlet 类实例?用到了什么原 理?
解决idea导入不了servlet类(1).docx
原生Servlet与Spring Controller性能比较 Controller项目
JSP Servlet性能分析.rar
这是对javaee 中servlet编程中涉及到的类的总体布局联系图,有了它可以帮你清楚的了解servlet的总体布局。希望能够帮到你!
韩顺平.2011最新j2ee视频教程.细说servlet用户管理系统工具类
里面有注释,使用的技术:Session,HashMap,List有易于帮助新手开阔思路.很简单的。
对java 的servlet类进行反射的处理,以方便servlet类的使用。一个简单的反射类的使用,非常实用,只需要俩个积分既可以进行使用。。
servlet常用类 Servlet常用接口
java Servlet对表单的处理过程 java Servlet对表单的处理过程 java Servlet对表单的处理过程
servlet与Struts action线程安全问题分析
servlet基础与servlet容器模型
java web servlet jstl 对 dept 及emp 表 进行 对 emp表 进行 增删改查 用 封装 继承 多态的方式 ,实现该功能 分层开发: entity 实体层 service 业务层接口及实现类 util 工具类 controller Servlet 充当
内容包括了Tomcat服务器配置、启动分析、Servlet文件配置等tomcat的基础应用配置。
一个servlet类处理多个请求(反射优化),简单易懂
Servlet详细讲解资料,包含案例分析,还有其他知识需求可以私信我!
servlet用到的类,Javaweb用到的类
最基础servlet例子,包含一个servlet类,一个web.xml文件的配置