一、web.xml
(1)指定过滤的Servlet类
(2)配置过滤规则,过滤以.mail结尾的链接
mailServlet com.zit.rfid.app.prms.business.service.servlet.MailServlet 2 mailServlet
二、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,重新跳转到该链接即可