Tomcat请求处理流程

简介: Tomcat+Struts1.x默认的ReqeustProcessor的请求处理流程Tomcat+Struts+Spring2.


Tomcat+Struts1.x默认的ReqeustProcessor的请求处理流程




Tomcat+Struts+Spring2.5.x提供的DelegatingRequestProcessor的请求处理流程




Struts1.x默认的RequestProcessor得到Action的方法:processActionCreate


processActionCreate(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping):
    /**
     * <p>Return an <code>Action</code> instance that will be used to process
     * the current request, creating a new one if necessary.</p>
     *
     * @param request  The servlet request we are processing
     * @param response The servlet response we are creating
     * @param mapping  The mapping we are using
     * @return An <code>Action</code> instance that will be used to process
     *         the current request.
     * @throws IOException if an input/output error occurs
     */
    protected Action processActionCreate(HttpServletRequest request,
        HttpServletResponse response, ActionMapping mapping)
        throws IOException {
        // Acquire the Action instance we will be using (if there is one)
        String className = mapping.getType();

        if (log.isDebugEnabled()) {
            log.debug(" Looking for Action instance for class " + className);
        }

        // If there were a mapping property indicating whether
        // an Action were a singleton or not ([true]),
        // could we just instantiate and return a new instance here?
        Action instance;

        synchronized (actions) {
            // Return any existing Action instance of this class
            instance = (Action) actions.get(className);

            if (instance != null) {
                if (log.isTraceEnabled()) {
                    log.trace("  Returning existing Action instance");
                }

                return (instance);
            }

            // Create and return a new Action instance
            if (log.isTraceEnabled()) {
                log.trace("  Creating new Action instance");
            }

            try {
                instance = (Action) RequestUtils.applicationInstance(className);

                // Maybe we should propagate this exception
                // instead of returning null.
            } catch (Exception e) {
                log.error(getInternal().getMessage("actionCreate",
                        mapping.getPath()), e);

                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                    getInternal().getMessage("actionCreate", mapping.getPath()));

                return (null);
            }

            actions.put(className, instance);
        }

        if (instance.getServlet() == null) {
            instance.setServlet(this.servlet);
        }

        return (instance);
    }

Spring的DelegatingRequestProcessor得到Action的方法:

processActionCreate(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping):
	/**
	 * Override the base class method to return the delegate action.
	 * @see #getDelegateAction
	 */
	protected Action processActionCreate(
			HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
			throws IOException {
                //先试图从Sping的Action容器中获取对应的Action,如果没有找到,再调用Strut提供的RequestProcessor的processActionCreate方法得到对应的Action。
		Action action = getDelegateAction(mapping);
		if (action != null) {
			return action;
		}//如果没有找到,再调用Strut提供的RequestProcessor的processActionCreate方法得到对应的Action。
		return super.processActionCreate(request, response, mapping);
	}


	/**
	 * Return the delegate <code>Action</code> for the given mapping.
	 * <p>The default implementation determines a bean name from the
	 * given <code>ActionMapping</code> and looks up the corresponding
	 * bean in the <code>WebApplicationContext</code>.
	 * @param mapping the Struts <code>ActionMapping</code>
	 * @return the delegate <code>Action</code>, or <code>null</code> if none found
	 * @throws BeansException if thrown by <code>WebApplicationContext</code> methods
	 * @see #determineActionBeanName
	 */
	protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
		String beanName = determineActionBeanName(mapping);
		if (!getWebApplicationContext().containsBean(beanName)) {
			return null;
		}
		return (Action) getWebApplicationContext().getBean(beanName, Action.class);
	}


目录
相关文章
|
5月前
|
应用服务中间件
从零手写实现 tomcat-03-请求和响应的抽象
该文档介绍了 MiniCat 项目,它是一个简单的 HTTP 服务器实现。v1 版本中, MiniCatRequest 对象解析 HTTP 请求,包括方法、URL 和输入流,而 MiniCatResponse 使用输出流处理响应。start 方法使用这些封装后的对象处理网络通信。在 v2 版本,服务器添加了返回静态资源文件的功能,如 HTML,通过解析 URL 并读取对应本地文件内容来响应请求。测试示例展示了如何访问和显示 index.html。
|
10月前
|
JSON 前端开发 Java
图解HTTP请求Tomcat服务器实现前后端交互-2
图解HTTP请求Tomcat服务器实现前后端交互
92 0
|
10月前
|
前端开发 JavaScript Java
图解HTTP请求Tomcat服务器实现前后端交互-1
图解HTTP请求Tomcat服务器实现前后端交互
180 0
|
21天前
|
监控 网络协议 应用服务中间件
【Tomcat源码分析】从零开始理解 HTTP 请求处理 (第一篇)
本文详细解析了Tomcat架构中复杂的`Connector`组件。作为客户端与服务器间沟通的桥梁,`Connector`负责接收请求、封装为`Request`和`Response`对象,并传递给`Container`处理。文章通过四个关键问题逐步剖析了`Connector`的工作原理,并深入探讨了其构造方法、`init()`与`start()`方法。通过分析`ProtocolHandler`、`Endpoint`等核心组件,揭示了`Connector`初始化及启动的全过程。本文适合希望深入了解Tomcat内部机制的读者。欢迎关注并点赞,持续更新中。如有问题,可搜索【码上遇见你】交流。
【Tomcat源码分析】从零开始理解 HTTP 请求处理 (第一篇)
|
4月前
|
缓存 负载均衡 NoSQL
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
73 1
|
4月前
|
应用服务中间件
tomcat服务器get、post请求及响应中文乱码问题
tomcat服务器get、post请求及响应中文乱码问题
|
4月前
|
Java 应用服务中间件 API
Tomcat处理一个HTTP请求的执行流程的详细解析
Tomcat处理一个HTTP请求的执行流程的详细解析
111 4
|
3月前
|
应用服务中间件
tomcat8.5处理get请求时,控制台输出中文乱码问题的解决
tomcat8.5处理get请求时,控制台输出中文乱码问题的解决
45 0
|
5月前
|
XML Java 应用服务中间件
SpringBoot配置外部Tomcat项目启动流程源码分析(长文)
SpringBoot配置外部Tomcat项目启动流程源码分析(长文)
412 0
|
前端开发 Java 应用服务中间件
TOMCAT 源码分析 -- 一次请求
TOMCAT 源码分析 -- 一次请求
95 0