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