public void doFilter(ServletRequest servletReqest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest hRequest = (HttpServletRequest) servletReqest;
HttpServletResponse hResponse = (HttpServletResponse) servletResponse;
if (this.sessionServerUrl != null) {
// 检测当前应用是否充当了会话服务器的职能,如果是,则直接进入下一个过滤器
boolean isSessionServerUrl = this.sessionServerUrl.replace(
hRequest.getRequestURL().toString(), "").trim().equals("");
if (isSessionServerUrl) {
filterChain.doFilter(hRequest, hResponse);
return;
}
HttpSession session = hRequest.getSession();
// 从当前会话中获取snaId
String snaId = (String) session
.getAttribute(SNAIdRequestServlet.SNA_ID);
if (snaId == null) {// 如果没有标识,则尝试从参数中获取
snaId = hRequest.getParameter(SNAIdRequestServlet.SNA_ID);
}
String queryString = hRequest.getQueryString();
// 如果snaId为null,则重定向到sna会话服务器,获取新的snaId
if (snaId == null || snaId.trim().equals("")) {
if (queryString != null && !queryString.equals(""))
queryString = "&" + queryString;
else
queryString = "";
// 从SNA会话服务器上获取snaId
String redirectUrl = this.sessionServerUrl + "?sourceUrl="
+ hRequest.getRequestURL() + ";jsessionid="
+ session.getId() + queryString;
hResponse.sendRedirect(redirectUrl);
return;
}
// 保存snaId到当前会话
if (session.getAttribute(SNAIdRequestServlet.SNA_ID) == null) {
session.setAttribute(SNAIdRequestServlet.SNA_ID, snaId);
// 构造查询参数
queryString = queryString.replaceAll(SNAIdRequestServlet.SNA_ID
+ "=" + snaId, "");
if (queryString.startsWith("&"))
queryString = queryString.replaceFirst("&", "?");
// 重定向回原始页面
hResponse.sendRedirect(hRequest.getRequestURL() + queryString);
return;
}
}
String uri = hRequest.getRequestURI();