这与上一个问题有关:How to intercept request in servlet filter first before it hits the login jsp in Spring MVC?
我做了一些研究,发现有一个SavedRequest对象可以保存原始URL,所以在我的login.jsp中我有以下几行:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ page import="org.springframework.security.web.savedrequest.*" %>
<%@ page import="java.io.*,java.util.*" %>
<%
System.out.println(" [ JSP = 1 ] "+new Date().getTime());
SavedRequest savedRequest=new HttpSessionRequestCache().getRequest(request,response);
System.out.println(" [ JSP = 1.1 ] savedRequest == null ? "+(savedRequest == null));
String requestUrl=savedRequest.getRedirectUrl();
System.out.println(" requestUrl = "+requestUrl);
%>
...
但输出是:
INFO: Server startup in 54732 ms
[ JSP = 1 ] 1407987538402
[ JSP = 1.1 ] savedRequest == null ? true
Aug 13, 2014 11:38:58 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
我在登录前点击了一个URL [类似:http://example.com/supportCenter/mainPage.html?token=XYZ],并被发送到login.jsp,所以在login.jsp中我希望获得原始网址[http://example.com/supportCenter/mainPage.html?token=XYZ]来自requestUrl,但savedRequest为null,为什么?