Home  >  Article  >  Web Front-end  >  Jump to login page when session expires_html/css_WEB-ITnose

Jump to login page when session expires_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:52:071448browse

The project needs to have an automatic logout function. I checked the information on the Internet and initially planned to use session monitoring. Configure the listener as follows

1. In the project's web.xml Add the following code to the file:

?

1

2

3

4

 监听器路径 

1 2

3

4

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public class SessionListener implements HttpSessionListener {

 public void sessionCreated(HttpSessionEvent arg0) {

  // session创建时执行  SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms");

  String nowtimes = simpleFormat.format(new Date());

  User u=null;

  //System.out.println("执行。。 当前时间:" nowtimes "_" u);  HttpSession ses= arg0.getSession();

  String id=ses.getId() "_" ses.getCreationTime();

 }

 public void sessionDestroyed(HttpSessionEvent arg0) {

  // session失效时执行  SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms");

  String nowtimes = simpleFormat.format(new Date()); 

  //System.out.println("session失效了。。 结束时间: " nowtimes); }

}

Listener path

2. Write a java class.

1

2

3

4

5

6

7

8

        sessionFilter

        com.orchestrall.web.helper.session.SessionFilter

        sessionFilter

        /actions/*

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class SessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent arg0) { // Executed when session is created SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms"); String nowtimes = simpleFormat.format(new Date()); User u=null; //System.out.println("Execute. . Current time: " nowtimes "_" u); HttpSession ses= arg0.getSession(); String id=ses.getId() "_" ses.getCreationTime(); } public void sessionDestroyed(HttpSessionEvent arg0) { // Executed when session fails SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms"); String nowtimes = simpleFormat.format(new Date()); //System.out.println("session has expired. End time: " nowtimes); } }
After the configuration is completed and the session expires, it successfully enters the sessionDestroyed method and is ready to perform the page jump operation. Suddenly, I found out how to write a jump and was stunned. I continued to go online to consult the experts and found that this monitoring was done for some background statistical processing and could not realize the page jump function. I had to give up this method and start using filters to achieve it. 1. Add filter configuration in web.xml ?
1 2 3 4 5 6 7 8 filter-class>com.orchestrall.web.helper.session.SessionFilter sessionFilter      /actions/*

2. Create a new SessionFilter class and implement the Filter interface.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

public class SessionFilterimplements Filter {

public void destroy() {

// TODO Auto-generated method stub }

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest httpRequest = (HttpServletRequest) request;

HttpServletResponse httpResponse = (HttpServletResponse) response; "/admin/login.jsp";

String url = httpRequest.getRequestURI();

String path = url.substring(url.lastIndexOf("/"));

// Timeout processing, ajax request timeout Set the timeout status. If the page request times out, a prompt will be returned and redirected if (path.indexOf(".action") != -1

// Determine whether it is an ajax request if (httpRequest.getHeader("x-requested-with") != null

&& httpRequest.getHeader("x-requested-with")

              .equalsIgnoreCase("XMLHttpRequest")) {

                                                             .

                     httpResponse.addHeader("loginPath", loginUrl); 🎜>

String str = "";

response.setContentType("text/html;charset=UTF-8");// Solve Chinese garbled characters Try {

PrintWriter writer = response.getWriter();

writer.write(str);

writer.flush();

             writer.close();

                                          (Exception                                                                                                                                                                                                                                                chain.doFilter(request, response);

}

}

@Override

public void init(FilterConfig arg0) throws ServletException {

                                                                                                                                                                                          

3. Client JS, used for ajax request session timeout

For jquery

?

1

2

3

4

5

6

7

8

9

10

11

12

1 2

3

4

5

1

2

3

4

5

6

7

8

9

10

11

Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);

    function checkUserSessionStatus(conn,response,options){

        if(response.getResponseHeader("sessionstatus") == 'timeout'){

            if(response.getResponseHeader("loginPath")){

                alert("会话过期,请重新登陆!");

                window.top.location.href = response.getResponseHeader("loginPath");

            }else{

                alert("请求超时请重新登陆 !");

            }

        }

    }

6

7

8

9

1

2

3

4

$.ajax({

    url:"test.html",

    global:false//不触发全局ajax事件})

10 11 12