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 }"
本文介绍了一种使用Cookie来存储和读取用户登录信息的方法,并展示了如何在Java Web应用中通过HttpServletRequest对象来实现这一过程。此外,还提供了一个简单的示例说明了如何在web.xml文件中配置拦截器来处理特定URL模式下的Cookie。
3520

被折叠的 条评论
为什么被折叠?



