private void getUser(String path, HttpServletRequest req) {
// TODO Auto-generated method stub
if(path!=null&&path.contains("/index.jsp")||path.contains("logout.do")){
String name="";
String pass="";
Cookie[] cookies = req.getCookies();
if(cookies!=null&&cookies.length>0){
for (Cookie cookie : cookies) {
if(cookie.getName().equals("cname")){
name=cookie.getValue();
}
if(cookie.getName().equals("cpass")){
pass=cookie.getValue();
}
}
}
req.setAttribute("cname", name);
req.setAttribute("cpass", pass);
}
}
// TODO Auto-generated method stub
if(path!=null&&path.contains("/index.jsp")||path.contains("logout.do")){
String name="";
String pass="";
Cookie[] cookies = req.getCookies();
if(cookies!=null&&cookies.length>0){
for (Cookie cookie : cookies) {
if(cookie.getName().equals("cname")){
name=cookie.getValue();
}
if(cookie.getName().equals("cpass")){
pass=cookie.getValue();
}
}
}
req.setAttribute("cname", name);
req.setAttribute("cpass", pass);
}
}
web.xml需要设置cookie的拦截器
<!-- 设置cookie -->
<filter>
<filter-name>system</filter-name>
<filter-class>com.elec.util.SytemFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>system</filter-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!--配置struts2的过滤器,这是struts2运行的核心 -->
jsp中获取cookie
value="${name }"