如何利用servlet来控制session失效
我自己做了一个servlet,在web.xml中配置如下:
<servlet>
<servlet-name> servlet1 </servlet-name>
<servlet-class> com.zwspace.support.Servlet1 </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> servlet1 </servlet-name>
<url-pattern> /*.do </url-pattern>
</servlet-mapping>
我的本意是当有.do的action提交时用servlet来判断session是否失效,如果失效就返回前一个页面
程序如下:
package com.zwspace.support;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.ServletRequestUtils;
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8 ";
//Initialize global variables
public void init() throws
ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
if(SessionHelper.getUser(request)==null){
try {
this.redirect(request, response);
} catch (Exception ex) {
}
}
out.close();
}
public ModelAndView redirect(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView( "redirect: " + request.getHeader( "referer "));
}
&nb