码迷,mamicode.com
首页 > 其他好文 > 详细

外部点击链接,登陆后,直接跳转到该链接(过滤器 + Cookie实现)

时间:2018-05-16 13:17:55      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:ide   过滤器   exce   extend   last   ram   vax   ext   url   

 

 

 

一、web.xml

(1)指定过滤的Servlet类

(2)配置过滤规则,过滤以.mail结尾的链接

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    

    <servlet>
        <servlet-name>mailServlet</servlet-name>
        <servlet-class>
            com.zit.rfid.app.prms.business.service.servlet.MailServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mailServlet</servlet-name>
        <url-pattern>*.mail</url-pattern>
    </servlet-mapping>
 
</web-app>

 

 

 

二、Servlet过滤类

  

  当链接中包含以.mail结尾的链接,就执行下面这个Servlet

package com.zit.rfid.app.prms.business.service.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MailServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        StringBuffer mailUrl = new StringBuffer();
        String reqUrl = request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf(".mail"));
        mailUrl.append(reqUrl)
                .append(".html?id=")
                 .append(request.getParameter("id"));
        Cookie cookie = new Cookie("mailUrl", mailUrl.toString());
        //生命周期:秒
        cookie.setMaxAge(3600);
        response.addCookie(cookie);
        response.sendRedirect(mailUrl.toString());
    }
    
}

 

(1) 获取该链接,重新组装链接地址(比如:把.mail换成.html,因为.mail是过滤规则)

(2)将该链接加入到Cookie

(3)在前台JS里,比如登陆成功后的函数里:获取该Cookie,重新跳转到该链接即可

 

外部点击链接,登陆后,直接跳转到该链接(过滤器 + Cookie实现)

标签:ide   过滤器   exce   extend   last   ram   vax   ext   url   

原文地址:https://www.cnblogs.com/Donnnnnn/p/9044710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!