001/* 002 * Copyright 2002-2012 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.servlet.handler; 018 019import javax.servlet.Servlet; 020import javax.servlet.http.HttpServletRequest; 021import javax.servlet.http.HttpServletResponse; 022 023import org.springframework.lang.Nullable; 024import org.springframework.web.servlet.HandlerAdapter; 025import org.springframework.web.servlet.ModelAndView; 026 027/** 028 * Adapter to use the Servlet interface with the generic DispatcherServlet. 029 * Calls the Servlet's {@code service} method to handle a request. 030 * 031 * <p>Last-modified checking is not explicitly supported: This is typically 032 * handled by the Servlet implementation itself (usually deriving from 033 * the HttpServlet base class). 034 * 035 * <p>This adapter is not activated by default; it needs to be defined as a 036 * bean in the DispatcherServlet context. It will automatically apply to 037 * mapped handler beans that implement the Servlet interface then. 038 * 039 * <p>Note that Servlet instances defined as bean will not receive initialization 040 * and destruction callbacks, unless a special post-processor such as 041 * SimpleServletPostProcessor is defined in the DispatcherServlet context. 042 * 043 * <p><b>Alternatively, consider wrapping a Servlet with Spring's 044 * ServletWrappingController.</b> This is particularly appropriate for 045 * existing Servlet classes, allowing to specify Servlet initialization 046 * parameters etc. 047 * 048 * @author Juergen Hoeller 049 * @since 1.1.5 050 * @see javax.servlet.Servlet 051 * @see javax.servlet.http.HttpServlet 052 * @see SimpleServletPostProcessor 053 * @see org.springframework.web.servlet.mvc.ServletWrappingController 054 */ 055public class SimpleServletHandlerAdapter implements HandlerAdapter { 056 057 @Override 058 public boolean supports(Object handler) { 059 return (handler instanceof Servlet); 060 } 061 062 @Override 063 @Nullable 064 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) 065 throws Exception { 066 067 ((Servlet) handler).service(request, response); 068 return null; 069 } 070 071 @Override 072 public long getLastModified(HttpServletRequest request, Object handler) { 073 return -1; 074 } 075 076}