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


    一、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,重新跳转到该链接即可

  • 相关阅读:
    开源项目:MMTweenAnimation
    URI跳转方式地图导航的代码实践
    处理i18n国际电话区号的代码实践
    图片变形的抗锯齿处理方法
    CocoaPods版本升级
    JSPatch 部署安全策略
    JSPatch实现原理详解<二>
    JSPatch实现原理详解
    JSPatch – 动态更新iOS APP
    iOS富文本组件的实现—DTCoreText源码解析 渲染篇
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/9044710.html
Copyright © 2020-2023  润新知