外部点击链接,登陆后,直接跳转到该链接(过滤器 + Cookie实现)
Posted 王晓东
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了外部点击链接,登陆后,直接跳转到该链接(过滤器 + 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,重新跳转到该链接即可
以上是关于外部点击链接,登陆后,直接跳转到该链接(过滤器 + Cookie实现)的主要内容,如果未能解决你的问题,请参考以下文章