首页 > 代码库 > 使用servlet原生API作为参数

使用servlet原生API作为参数

SpringMVC的Handler方法可以使用servlet原生的API作为目标方法的参数,具体支持以下类型:

* HttpServletRequest

* HttpServletResponse

* HttpSession

* java.security.Principal

* Locale InputStream

* OutputStream

* Reader

* Writer

此时,需要在pom.xml增加如下配置(别忘了版本<servlet-api.version>3.1.0</servlet-api.version>):

1 <!-- servlet -->
2         <dependency>
3             <groupId>javax.servlet</groupId>
4             <artifactId>javax.servlet-api</artifactId>
5             <version>${servlet-api.version}</version>
6             <scope>provided</scope>
7         </dependency>
 1 @RequestMapping("/springmvc")
 2 @Controller
 3 public class SpringMVCTest {
 4     private static final String SUCCESS = "success";
 5     @RequestMapping("/testServletAPI")
 6     public void testServletAPI(HttpServletRequest request,HttpServletResponse response,Writer out) throws IOException {
 7         System.out.println("testServletAPI," + request + "," + response);
 8         out.write("hello");
 9         //return SUCCESS;
10     }

运行结果:

           在页面中会打印出hello  在console中:

           testServletAPI,org.apache.catalina.connector.RequestFacade@6dfcd74,org.apache.catalina.connector.ResponseFacade@fd25fbe

使用servlet原生API作为参数