你扩展RequestProcess类就可以了。
让session失效的话,在tomcat中设置就行了,在conf目录下的web.xml中
<session-config>
<session-timeout>30</session-timeout>
</session-config>
让session失效的话,在tomcat中设置就行了,在conf目录下的web.xml中
<session-config>
<session-timeout>30</session-timeout>
</session-config>
HttpSession session=request.getSession(false);
if (session==null)
response.sendRedirect("Index.jsp");HttpSession类有一个方法设置失效时间 通过该方法设置最大失效时间为15×60就可以了
good luck~:)
1.在这个类中,只要对processPreprocess方法进行相对应的扩充,就可以了。
public class MyRequestProcessor extends RequestProcessor{
/**
* <p>Process an <code>HttpServletRequest</code> and create the
* corresponding <code>HttpServletResponse</code>.</p>
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a processing exception occurs
*/
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException { // Wrap multipart requests with a special wrapper
request = processMultipart(request); // Identify the path component we will use to select a mapping String path = processPath(request, response);
System.out.println("33333333333333333333333="+path);
if (path == null) {
return;
}
if (log.isDebugEnabled()) {
log.debug("Processing a '" + request.getMethod() +
"' for path '" + path + "'");
} // Select a Locale for the current user if requested
processLocale(request, response); // Set the content type and no-caching headers if requested
processContent(request, response);
processNoCache(request, response); // General purpose preprocessing hook
if (!processPreprocess(request, response)) {
path="/fail";
System.out.println("11111111111="+path);
//return;
} // Identify the mapping for this request
ActionMapping mapping = processMapping(request, response, path);
if (mapping == null) {
return;
} // Check for any role required to perform this action
if (!processRoles(request, response, mapping)) {
return;
} // Process any ActionForm bean related to this request
ActionForm form = processActionForm(request, response, mapping);
processPopulate(request, response, form, mapping);
if (!processValidate(request, response, form, mapping)) {
return;
} // Process a forward or include specified by this mapping
if (!processForward(request, response, mapping)) {
return;
}
if (!processInclude(request, response, mapping)) {
return;
} // Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
} // Call the Action instance itself
ActionForward forward =
processActionPerform(request, response,
action, form, mapping); // Process the returned ActionForward instance
processForwardConfig(request, response, forward); }
/**
* General-purpose preprocessing hook that can be overridden as required
* by subclasses. Return <code>true</code> if you want standard processing
* to continue, or <code>false</code> if the response has already been
* completed. The default implementation does nothing.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*/ protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession();
//String ss = (String) session.getAttribute("ss");
String ss="sdfds";
System.out.println("---------------------processPreprocess=" + ss);
if (ss != null) {
return (true);
}else {
return false;
} }}
2。在struts-config.xml中的配置
<controller processorClass="com.mystruts.MyRequestProcessor" />
return actionMapping.findForward(你要转向的指定页面);
2)如楼上,把30改为15就满足你的要求了。
我想该用个监听器吧
在process方法中有调用processPreprocess的地方。
if (!processPreprocess(request, response)) {
path="/fail";
System.out.println("11111111111="+path);
//return;
}
你在这里的path设为你想要跳转的路径就行了。
HttpSession session=request.getSession(false);
if (session==null)
response.sendRedirect("***.jsp");
if (session!=null) {
return (true);
}else {
return false;
}
而这段代码的else永远都不会执行。
session失效时会激活valueUnbound事件,你在这里面控制就可以了。
import javax.servlet.http.HttpSessionBindingListener;
public class UserTrace implements HttpSessionBindingListener { //当UserTrace被加入session对象时会调用此方法
public void valueBound(HttpSessionBindingEvent event) { }
//当UserTrace被移出session时会调用此方法
public void valueUnbound(HttpSessionBindingEvent event) { }}
这是我写的一个在线用户功能的片断(程序框架),你可以参照看看。
具体的方法你就自己写了。
session.setAttribute("Binding",new HttpSessionBindingListener);
其中new HttpSessionBindingListener是你实现该接口的类的实例
<session-timeout>30</session-timeout>
</session-config>
可以设置有效时间。
<jsp:useBean id="usertrace" class="com.xx.xx.UserTrace" scope="session"/>
HttpSession session=request.getSession(false);
if (session!=null) {
return (true);
}else {//我这里也是永远不执行,为什么啊????
return false;
}
然后打印出来的sessionId都是一个新id,为什么session=request.getSession(false);参数是false,居然还给我创建了新session??????????????????????
public void valueUnbound(HttpSessionBindingEvent event) {
//如何说失效时候跳转到xx.jsp???
}
{
..跳转到你要去的页面
}
strusts中我不知道,我只能告诉你平时的方法关于如何判断SESSION过期就不说了,主要说说在 过期后的处理方式。一、使用javascript提示然后再跳转
<%
if(.....){//SESSION过期
%>
<body onload="alert('session timeout.');window.locaitin='XXXXXX'">
</body>
<%
return;
}
%>
二、直接再后台跳转。
<%
if(.....){//SESSION过期
response.sendRedirect("XXXXXXX");
}
%>
-------------------web.xml配置:
<filter>
<filter-name>mainfilter</filter-name>
<filter-class>com.chance.MainFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>mainfilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
------------------MainFilter.java--------------------------
package com.chance;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import java.io.IOException;public class MainFilter implements Filter{
public void init(FilterConfig filterConfig)throws ServletException{
//
} public void doFilter(ServletRequest request,
ServletResponse response,FilterChain chain)throws ServletException, IOException{
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
boolean isValid=true;
///////check attribute
isValid=req.getSession().getAttribute("name")==null;
if(isValid){
req.getSession().getServletContext()
.getRequestDispatcher("/login.jsp").forward(req, res);
}else
chain.doFilter(request,response);
} public void destroy(){}
}
public class MyHttpSessionAttributeListener
implements HttpSessionAttributeListener,HttpSessionListener { /**
*
*/
private static List list=new ArrayList();
public MyHttpSessionAttributeListener() {
super();
// TODO Auto-generated constructor stub
} /* (non-Javadoc)
* @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
*/
public void attributeAdded(HttpSessionBindingEvent se) {
if("userInfo".equals(se.getName()))
{
UserForm user=(UserForm)se.getValue();
Iterator userList=list.iterator();
if(!list.contains(user.getUserId()))
{
//list.add(se.getValue());
list.add(user.getUserId());
}else
{
// se.getSession().invalidate();
// se.getSession().setAttribute("userInfo",user);
System.out.println("重复登陆");
// throw new Exception("重复登陆");
}
System.out.println("user added:"+se.getValue());
} } /* (non-Javadoc)
* @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
*/
public void attributeRemoved(HttpSessionBindingEvent se) {
if("userInfo".equals(se.getName()))
{
String userId=((UserForm)se.getValue()).getUnitId();
list.remove(userId);
System.out.println("user removed:"+userId);
}
} /* (non-Javadoc)
* @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
*/
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub } /* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
} /* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent se) {
String userId = (String)se.getSession().getAttribute("userid");
list.remove(userId);
System.out.println("user removed:"+userId);
}}但不知道一个帐号不能同时两个人用怎么做?而在这个类中能跳转到其它页面吗?
.getRequestDispatcher("/login.jsp").forward(req, res);
而不会继续执行后面的代码!
我上面说的方法只能检测到SESSION过期,但是过期了怎么让客户端页面跳转?好像办不到?
过期了怎么让客户端页面跳转???如果用户关掉浏览器使得SESSION过期,更加不可能是他跳转了吧?