Cookie存储url防止转义,后台解析url执行ulr重定向,跳转指定的url

1、当前浏览器url

http://localhost:8081/login?refer=http%3A%2F%2Flocalhost%3A8081%2Fmember%2Fuser%2FuserMessage%2Flist

2、通过js将当前url写入cookie

/**
 * 将跳转的url写入cookie
 */
function addUrl(){
	 var href=window.location.href;
	 if(href.indexOf("refer")!=-1){
		var str=decodeURIComponent($.trim(href.split("refer=")[1])); 
	    $.cookie('callbackUrl', str,{path:'/'});
	 }
}

3、后台解析url,执行跳转

//后台先从cookie取出url,执行解析并跳转
		String callBack_url=null;
		if(cookies!=null&&cookies.length>0){
		for (Cookie cookie : cookies) {
		    if(cookie.getName().equals("callbackUrl")){
  	    	     callBack_url=cookie.getValue();
  	    	     continue;
  	        }
		 }
		}
	    if(callBack_url!=null){
	       callBack_url=URLDecoder.decode(callBack_url, "UTF-8");
		   response.sendRedirect(callBack_url);//直接跳转
		}



你可能感兴趣的:(java)