001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web; 018 019import java.io.IOException; 020 021import javax.servlet.ServletException; 022import javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024 025/** 026 * Plain handler interface for components that process HTTP requests, 027 * analogous to a Servlet. Only declares {@link javax.servlet.ServletException} 028 * and {@link java.io.IOException}, to allow for usage within any 029 * {@link javax.servlet.http.HttpServlet}. This interface is essentially the 030 * direct equivalent of an HttpServlet, reduced to a central handle method. 031 * 032 * <p>The easiest way to expose an HttpRequestHandler bean in Spring style 033 * is to define it in Spring's root web application context and define 034 * an {@link org.springframework.web.context.support.HttpRequestHandlerServlet} 035 * in {@code web.xml}, pointing to the target HttpRequestHandler bean 036 * through its {@code servlet-name} which needs to match the target bean name. 037 * 038 * <p>Supported as a handler type within Spring's 039 * {@link org.springframework.web.servlet.DispatcherServlet}, being able 040 * to interact with the dispatcher's advanced mapping and interception 041 * facilities. This is the recommended way of exposing an HttpRequestHandler, 042 * while keeping the handler implementations free of direct dependencies 043 * on a DispatcherServlet environment. 044 * 045 * <p>Typically implemented to generate binary responses directly, 046 * with no separate view resource involved. This differentiates it from a 047 * {@link org.springframework.web.servlet.mvc.Controller} within Spring's Web MVC 048 * framework. The lack of a {@link org.springframework.web.servlet.ModelAndView} 049 * return value gives a clearer signature to callers other than the 050 * DispatcherServlet, indicating that there will never be a view to render. 051 * 052 * <p>As of Spring 2.0, Spring's HTTP-based remote exporters, such as 053 * {@link org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter} 054 * and {@link org.springframework.remoting.caucho.HessianServiceExporter}, 055 * implement this interface rather than the more extensive Controller interface, 056 * for minimal dependencies on Spring-specific web infrastructure. 057 * 058 * <p>Note that HttpRequestHandlers may optionally implement the 059 * {@link org.springframework.web.servlet.mvc.LastModified} interface, 060 * just like Controllers can, <i>provided that they run within Spring's 061 * DispatcherServlet</i>. However, this is usually not necessary, since 062 * HttpRequestHandlers typically only support POST requests to begin with. 063 * Alternatively, a handler may implement the "If-Modified-Since" HTTP 064 * header processing manually within its {@code handle} method. 065 * 066 * @author Juergen Hoeller 067 * @since 2.0 068 * @see org.springframework.web.context.support.HttpRequestHandlerServlet 069 * @see org.springframework.web.servlet.DispatcherServlet 070 * @see org.springframework.web.servlet.ModelAndView 071 * @see org.springframework.web.servlet.mvc.Controller 072 * @see org.springframework.web.servlet.mvc.LastModified 073 * @see org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter 074 * @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter 075 * @see org.springframework.remoting.caucho.HessianServiceExporter 076 */ 077@FunctionalInterface 078public interface HttpRequestHandler { 079 080 /** 081 * Process the given request, generating a response. 082 * @param request current HTTP request 083 * @param response current HTTP response 084 * @throws ServletException in case of general errors 085 * @throws IOException in case of I/O errors 086 */ 087 void handleRequest(HttpServletRequest request, HttpServletResponse response) 088 throws ServletException, IOException; 089 090}