日期:2014-05-19  浏览次数:20840 次

action实现注销问题
struts2中,action方法里获取到的map类型的session,并把值已经存入。现在要实现注销logout,执行下面代码后:
///////////////////////////////////注销方法
public class Logout extends ActionSupport {

private ActionContext context;
private Map session;
//private HttpSession session;
 
public String logout() throws Exception {
this.context = ActionContext.getContext();
///this.session = (HttpSession)this.context.getSession();不能强转为httpsession
this.session = (Map)this.context.getSession();
/* session.clear();
System.out.println(session.isEmpty());*/
session.remove("username");
System.out.println(session.isEmpty());
//session.removeAttribute("username");
//session.invalidate();
 
return "logout";
}

}
//////////////////////////////////过滤器
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpSession session=((HttpServletRequest) request).getSession(true);
if(session.getAttribute("username")== null){
System.out.println("你还未登陆");
//String url = "http://localhost:8888/Test_Online/admin/login.jsp";
String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/test_struts2/login.jsp";
  //String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + request.getContextPath() + LoginRightFilter.FORWARD; 
StringBuffer scriptString=new StringBuffer(); 
  scriptString.append(" <script>\n\r"); 
  scriptString.append("alert(\"你还未登陆\")");
  scriptString.append("self.top.location.href=\""+url+"\"\n\r"); 
  scriptString.append(" </script>\n\r"); 
  response.getOutputStream().print(scriptString.toString()); 
  //System.out.println(session.getAttribute("adminrole"));
}else{

chain.doFilter(request, response);
}

}
我又进入一个新的页面(已设置过滤器),报的错是:
java.io.CharConversionException: Not an ISO 8859-1 character: 你
at javax.servlet.ServletOutputStream.print(ServletOutputStream.java:89)
at filter.LegalUser.doFilter(LegalUser.java:34)
at org.apache.catalina.core.ApplicationFilterChain
好像session里有记录,还是有cookies的?没清除网页自动记忆的用户名“你”,(我曾经输过的)不懂???

------解决方案--------------------
让session失效呢
不就注销了吗
------解决方案--------------------
response.setContentType("text/html;charset=utf-8");
response.getOutputStream().write(scriptString.toString().getBytes("UTF-8"));
------解决方案--------------------
编码的事,同楼上