映射 /check 到方法check()
为方法check()提供参数HttpSession session,这样就可以在方法体中使用session了
接下来的逻辑就是每次访问为session中的count+1.
最后跳转到check.jsp页面
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@RequestMapping("/check")
public ModelAndView check(HttpSession session){
Integer i = (Integer) session.getAttribute("count");
if(i==null){
i=0;
}
i++;
session.setAttribute("count", i);
ModelAndView mav = new ModelAndView("check");
return mav;
}
}
check.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>check</title>
</head>
<body>
session中记录的访问次数: ${count}
</body>
</html>