利用Spring IOC机制编写HttpServletUtil工具
工具目的:
使用servlet的缺点:
- 一个servlet类只能写一个请求方法(当然可以通过判断参数实现不同的请求),代码冗余
- 只能在doGet、doPost或者service方法中编写
SpringMVC的不足之处:
- 编写的SpringMVC可以使用Model,ModelAndView,ModelMap等对象进行传参,
- 但是!!!使用ModelAndView方法的放回类型必须的ModelAndView,使用Model和ModelMap必须写在方法的参数中
- 使用原生的servlet API request和session传参必须在方法的参数中声明才能使用。
因此,使用HttpServletUtil配合SpringMVC可以在SpringMVC类中通过注解注入工具类直接使用request对象、response对象、application对象和session对象,并且不用把这四个对象放在方法中声明便可以传参。
1、在springMVC的配置文件中添加具有HTTP协议的bean和开启springmvc的注解功能
<!-- 1. 扫描具有HTTP协议的注解bean -->
<context:component-scan base-package="com.gxwz.web" />
<!-- 2. 开启springmvc的注解功能 -->
<mvc:annotation-driven />
2、编写工具类 HttpServletUtil.java
package com.gxwz.util;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class HttpServletUtil {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
@Autowired
private HttpSession session;
@Autowired
private ServletContext application;
public HttpServletRequest getRequest() {
return request;
}
public HttpServletResponse getResponse() {
return response;
}
public HttpSession getSession() {
//return request.getSession();
return session;
}
public ServletContext getApplication() {
return application;
}
}
3、编写测试类 web.java
package com.gxwz.web;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gxwz.util.HttpServletUtil;
@Controller
public class Web{
@Autowired
private HttpServletUtil httpServletUtil;
@RequestMapping("/web")
public String service() {
List<String> list = new ArrayList<String>();
list.add("公司大法官");
list.add("国史大纲");
list.add("根深蒂固");
HttpServletRequest request = httpServletUtil.getRequest();
HttpServletResponse response = httpServletUtil.getResponse();
HttpSession session = httpServletUtil.getSession();
ServletContext application = httpServletUtil.getApplication();
System.out.println("request:"+request);
System.out.println("response:"+response);
System.out.println("application:"+application);
System.out.println("sessionId:"+session.getId());
request.setAttribute("list", list);
session.setAttribute("list", list);
application.setAttribute("list", list);
return "web.jsp";
}
}
4、编写跳转页面 web.jsp 测试效果
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- -->
<h2>欢迎访问SpringMVC测试网页</h2>
<hr>
<b>使用EL表达式获取:</b> ${list }
<hr>
<b>使用pageContext获取:</b> ${pageContextScope.list }
<hr>
<b>使用application获取:</b> ${applicationScope.list }
<hr>
<b>使用request获取:</b> ${requestScope.list }
<hr>
<b>使用session获取:</b> ${sessionScope.list }
<hr>
</body>
</html>
5、运行效果
总结:
这样就可以使用工具直接使用servlet的三大作用域的对象了,是不是很方便!!!