国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Web App struts框架里實現Filter

2019-11-18 15:54:46
字體:
來源:轉載
供稿:網友

  1 web.xml里添加filter配置信息。
  <filter>
  <filter-name>Authentication</filter-name>
  <filter-class>
  com.nova.colimas.web.filters.AuthenticationFilter
  </filter-class>
  <init-param>
  <param-name>onError</param-name>
  <param-value>/pages/index.jsp</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>Authentication</filter-name>
  <url-pattern>/protect/*</url-pattern>
  </filter-mapping>
  <filter-class>定義使用com.nova.colimas.web.filters.AuthenticationFilter類執行過濾Action。
  <init-param>當驗證失敗后Forward到/pages/index.jsp
  <filter-mapping>只有當地址包括/PRotect/時運行filter類
  
  2 實現com.nova.colimas.web.filters.AuthenticationFilter
  package com.nova.colimas.web.filters;
  import java.io.IOException;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.*;
  import javax.servlet.Filter;import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import com.nova.colimas.web.bean.UserBean;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;import org.apache.struts.Globals;
  import org.apache.struts.action.*;
  import com.nova.colimas.web.constants.Constants;
  /**
  * Filter implementation for user authentication。必須實現Filter接口
  * @author tyrone * @version $Id: AuthenticationFilter.java,v 1.0
  */public class AuthenticationFilter implements Filter{
  /**
  * filterConfig will store the reference of FilterConfig
  */  private FilterConfig filterConfig;
  private String onErrorUrl;
  /**
  * store the reference of the FilterConfig.
  * @param config FilterConfig object
  * @throws ServletException
  */  public void init(FilterConfig config) throws ServletException
  {//獲得驗證失敗forward地址
  filterConfig = config;
  onErrorUrl=filterConfig.getInitParameter("onError");
  if (onErrorUrl==null "".equals(onErrorUrl)){
  onErrorUrl="/pages/index.jsp";
  }
  }
  /**
  * User Authentication is done. If User is authenticated sUCcessful then
  * control is transferred to logon URI
  * @param ServletRequest Request
  * @param ServletRequest Response
  * @param FilterChain Filter Chain
  * @throws ServletException,IOException
  */  public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
  throws IOException, ServletException
  {
  HttpServletRequest httpRequest = (HttpServletRequest)request;
  HttpServletResponse httpResponse=(HttpServletResponse)response;
  // Current session
  HttpSession httpSession = httpRequest.getSession();
  //Session里是否有用戶信息。
  if (httpSession.getAttribute(Constants.USER_KEY) == null)
  {
  ActionErrors errors=new ActionErrors();
  errors.add(ActionErrors.GLOBAL_ERROR,
  new ActionError("error authentication"));
  httpRequest.setAttribute(Globals.ERROR_KEY,errors);
  //沒有,驗證失敗forward到/pages/index.jsp
  httpRequest.getRequestDispatcher(onErrorUrl).forward(httpRequest,httpResponse);
  }else//成果過濾Action結束
  next.doFilter(request,response);
  }
  /**
  * destroy() method is called by the servlet container
  */  public void destroy()
  {
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郸城县| 保山市| 乐山市| 阿瓦提县| 芦溪县| 仪征市| 宁明县| 清流县| 故城县| 绍兴县| 长泰县| 台南市| 泰来县| 盐源县| 视频| 忻城县| 濮阳县| 电白县| 巴彦淖尔市| 康平县| 合阳县| 尤溪县| 罗平县| 甘南县| 崇文区| 沭阳县| 界首市| 南漳县| 伊宁市| 宜兰市| 苏尼特右旗| 策勒县| 河池市| 垣曲县| 永春县| 扶绥县| 安多县| 柞水县| 凉城县| 怀远县| 平阳县|