登录超时给出提示跳到登录页面(ajax、导入、导出)
程序员文章站
2022-08-10 17:17:35
一、一般页面登录超时验证,可以用过滤器filter,如下:
package com.lg.filter;
import java.io.ioexceptio...
一、一般页面登录超时验证,可以用过滤器filter,如下:
package com.lg.filter; import java.io.ioexception; import javax.servlet.filter; import javax.servlet.filterchain; import javax.servlet.filterconfig; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.lg.func.myfunc; public class loginfilter implements filter{ public void destroy() { } public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws servletexception, ioexception { httpservletrequest request1=(httpservletrequest)request; httpservletresponse response1=(httpservletresponse)response; chain.dofilter(request, response);//放行。让其走到下个链或目标资源中 string url=request1.getservletpath(); system.out.println("demo1过滤前"+url); myfunc myfunc = new myfunc(request1,response1); system.out.println("demo1过滤前"+url.startswith("/index/")); if(myfunc.checklogin2()&&!url.startswith("/index/")){ response1.sendredirect("/index_login.html"); } system.out.println("demo1过滤后"); } public void init(filterconfig arg0) throws servletexception { // todo auto-generated method stub system.out.println("===========init========过滤后"); } }
web.xml配置
<filter> <filter-name>demo1filter</filter-name> <filter-class>com.lg.filter.loginfilter</filter-class> </filter> <filter-mapping> <filter-name>demo1filter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
二、ajax提交
提交页面,我的页面提示弹出框架用的asyncbox,可以改成其他的跳转
$.ajax({ type: "post", url:"a.jsp", async:false,//同步 data:{"name":"fdgh"} success:function(msg){ //checklogin2(msg)判断是否登录超时,超时返回false,跳到登录页面 if(checklogin2(msg)){ var obj=eval('('+msg+')'); if(obj.result.indexof("suc")>-1){ alert("成功"); }else{ alert("失败"); } } }); //是否登录超时,超时返回false,跳到登录页面 function checklogin2(msg){ if(msg!=null&&msg.length>0){ if(msg.indexof("doctype")>-1){ checklogin(); return false; } } return true; } function checklogin(){ if(window.top != window.self){ top.asyncbox.alert('登录超时,请重新登录', '提示', function(action){ top.location.href='/login.jsp'; }); }else{ asyncbox.alert('登录超时,请重新登录', '提示', function(action){ window.location.href='/login.jsp'; }); } }
后台:
1.处理数据前
if(checklogin())return; //检查登录,session过期,或者未登录,自动跳转 public boolean checklogin() throws ioexception{ boolean result = false; string html = ""; nativeobject u = sessionmng.getcurrentuser(request);//检验是否登录超时 if (u == null){ html = "<!doctype html public '-//w3c//dtd html 4.01 transitional//en' 'http://www.w3.org/tr/html4/loose.dtd'>\n" + "<script type='text/javascript' src='/admin/js/jquery.min.js'></script>\n" + "<script type='text/javascript' src='/admin/js/common.js'></script>\n" + "<script type='text/javascript' src='/admin/js/dialog/asyncbox.js'></script>\n" + "<script language=javascript>checklogin();</script>\n"; response.getwriter().println(html); result = true; } return result; }
三、异步导入excel
用ajaxupload.js导入excel功能
前端提交页面参考上面的;
后台处理页面:
if(!islogin()){ response.getwriter().print("doctype"); return ; } //是否登录 public boolean islogin(){ nativeobject u = sessionmng.getcurrentuser(request); if (u != null){ return true; }else{ return false; } }
四。用window.open导出excel文件
后台同二
前端导出页面
function export_excel(){ $.ajax({ type: "post", url:"/admin/inc/checklogin.jsp", async:false,//同步 success:function(msg){ if(checklogin2(msg)){ window.open("perfm_excel.jsp?"+$('#form1').serialize()); } } }); login.jsp <%@ page contenttype="text/html; charset=utf-8"%> <% //========================当前登陆用户信息======================================== if(checklogin())return; %>
以上内容给大家介绍了登录超时给出提示跳到登录页面(ajax、导入、导出)的相关知识,希望对大家有所帮助!
上一篇: Ajax发送和接收请求
下一篇: 浅谈Ajax技术实现页面无刷新