您的位置:首页 > 业界新闻

过滤器的正确打开方式

2015-07-07 23:38 615 查看
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
<display-name>ROOT</display-name>
<!--权限拦截器-->
<filter>
<filter-name>LimitsFilter</filter-name>
<filter-class>com.lg.filter.LimitsFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--初始化指定需要进行特殊权限拦截的页面-->
<init-param>
<param-name>ZXLOVE</param-name>
<param-value>/jsp/list/zxnet/zxlove/zxlove.htm</param-value>
</init-param>
<init-param>
<param-name>ZXSB</param-name>
<param-value>/jsp/list/zxnet/zxsb/1.html</param-value>
</init-param>
<init-param>
<param-name>CZ</param-name>
<param-value>/jsp/list/zxnet/cz/1.html</param-value>
</init-param>
<init-param>
<param-name>QLS<
20000
/span></param-name>
<param-value>/jsp/list/photos/qls/index.html</param-value>
</init-param>
<init-param>
<param-name>XDH</param-name>
<param-value>/jsp/list/photos/xdh/index.html</param-value>
</init-param>
<init-param>
<param-name>MA</param-name>
<param-value>/jsp/others/backmanage/index.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LimitsFilter</filter-name>
<url-pattern>/jsp/others/about/*</url-pattern>
<url-pattern>/jsp/others/backmanage/*</url-pattern>
<url-pattern>/jsp/others/blog/*</url-pattern>
<url-pattern>/jsp/others/share/main.jsp</url-pattern>
<url-pattern>/jsp/others/webnote/*</url-pattern>
<url-pattern>/jsp/others/ChatingRoom/index.jsp</url-pattern>
<url-pattern>/jsp/list/zxnet/*</url-pattern>
<url-pattern>/jsp/list/photos/*</url-pattern>
</filter-mapping>
<!--聊天室字符集过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.lg.chat.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!--监听器注册-->
<listener>
		<listener-class>
			com.lg.chat.servlet.UserListener
		</listener-class>
</listener>
<!--设置站内session默认生命周期:10min-->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<!--站内Error指定页面跳转-->
<error-page>
<error-code>400</error-code>
<location>/ehtml/400.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/ehtml/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/ehtml/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/ehtml/500.jsp</location>
</error-page>
<error-page>
<error-code>502</error-code>
<location>/ehtml/502.jsp</location>
</error-page>
<error-page>
<error-code>503</error-code>
<location>/ehtml/503.jsp</location>
</error-page>
<error-page>
<error-code>504</error-code>
<location>/ehtml/504.jsp</location>
</error-page>
<error-page>
<error-code>505</error-code>
<location>/ehtml/505.jsp</location>
</error-page>
<!--<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/ehtml/nullerror.html</location>
</error-page>
<error-page>
<exception-type>java.lang.IllegalArgumentException</exception-type>
<location>/ehtml/nullerror.html</location>
</error-page>-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
[/code]
packagecom.lg.filter;
importcom.lg.entity.User;
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
publicclassLimitsFilter
implementsFilter
{
privateFilterConfigconfig;
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)
throwsIOException,ServletException
{
Stringencoding=this.config.getInitParameter("encoding");
StringZXLOVE=this.config.getInitParameter("ZXLOVE");
StringZXSB=this.config.getInitParameter("ZXSB");
StringCZ=this.config.getInitParameter("CZ");
StringQLS=this.config.getInitParameter("QLS");
StringXDH=this.config.getInitParameter("XDH");
StringMA=this.config.getInitParameter("MA");
request.setCharacterEncoding(encoding);
HttpServletRequestrequ=(HttpServletRequest)request;
HttpSessionsession=requ.getSession(true);
StringrequestPath=requ.getServletPath();
if(session.getAttribute("user")==null)
{
request.getRequestDispatcher("/ehtml/nologin.jsp").forward(request,response);
}
elseif((session.getAttribute("user")!=null)&&((requestPath.endsWith(ZXLOVE))||(requestPath.endsWith(ZXSB))||(requestPath.endsWith(CZ))||(requestPath.endsWith(QLS))||(requestPath.endsWith(XDH))||(requestPath.endsWith(MA)))){
Useruser=(User)session.getAttribute("user");
if("A".equals(user.getQx()))
chain.doFilter(request,response);
else
request.getRequestDispatcher("/ehtml/notenoughqx.jsp").forward(request,response);
}
elseif((session.getAttribute("user")!=null)&&(!requestPath.endsWith(ZXLOVE))&&(!requestPath.endsWith(ZXSB))&&(!requestPath.endsWith(CZ))&&(!requestPath.endsWith(QLS))&&(!requestPath.endsWith(XDH))&&(!requestPath.endsWith(MA))){
chain.doFilter(request,response);
}else{
request.getRequestDispatcher("/ehtml/500.jsp").forward(request,response);
}
}
publicvoidinit(FilterConfigconfig)
throwsServletException
{
this.config=config;
}
publicvoiddestroy(){
this.config=null;
}
}
[/code]
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
<display-name>ROOT</display-name>
<!--权限拦截器-->
<filter>
<filter-name>LimitsFilter</filter-name>
<filter-class>com.lg.filter.LimitsFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--初始化指定需要进行特殊权限拦截的页面-->
<init-param>
<param-name>ZXLOVE</param-name>
<param-value>/jsp/list/zxnet/zxlove/zxlove.htm</param-value>
</init-param>
<init-param>
<param-name>ZXSB</param-name>
<param-value>/jsp/list/zxnet/zxsb/1.html</param-value>
</init-param>
<init-param>
<param-name>CZ</param-name>
<param-value>/jsp/list/zxnet/cz/1.html</param-value>
</init-param>
<init-param>
<param-name>QLS</param-name>
<param-value>/jsp/list/photos/qls/index.html</param-value>
</init-param>
<init-param>
<param-name>XDH</param-name>
<param-value>/jsp/list/photos/xdh/index.html</param-value>
</init-param>
<init-param>
<param-name>MA</param-name>
<param-value>/jsp/others/backmanage/index.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LimitsFilter</filter-name>
<url-pattern>/jsp/others/about/*</url-pattern>
<url-pattern>/jsp/others/backmanage/*</url-pattern>
<url-pattern>/jsp/others/blog/*</url-pattern>
<url-pattern>/jsp/others/share/main.jsp</url-pattern>
<url-pattern>/jsp/others/webnote/*</url-pattern>
<url-pattern>/jsp/others/ChatingRoom/index.jsp</url-pattern>
<url-pattern>/jsp/list/zxnet/*</url-pattern>
<url-pattern>/jsp/list/photos/*</url-pattern>
</filter-mapping>
<!--聊天室字符集过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.lg.chat.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<!--监听器注册-->
<listener>
		<listener-class>
			com.lg.chat.servlet.UserListener
		</listener-class>
</listener>
<!--设置站内session默认生命周期:10min-->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<!--站内Error指定页面跳转-->
<error-page>
<error-code>400</error-code>
<location>/ehtml/400.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/ehtml/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/ehtml/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/ehtml/500.jsp</location>
</error-page>
<error-page>
<error-code>502</error-code>
<location>/ehtml/502.jsp</location>
</error-page>
<error-page>
<error-code>503</error-code>
<location>/ehtml/503.jsp</location>
</error-page>
<error-page>
<error-code>504</error-code>
<location>/ehtml/504.jsp</location>
</error-page>
<error-page>
<error-code>505</error-code>
<location>/ehtml/505.jsp</location>
</error-page>
<!--<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/ehtml/nullerror.html</location>
</error-page>
<error-page>
<exception-type>java.lang.IllegalArgumentException</exception-type>
<location>/ehtml/nullerror.html</location>
</error-page>-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
[/code]
packagecom.lg.filter;
importcom.lg.entity.User;
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
publicclassLimitsFilter
implementsFilter
{
privateFilterConfigconfig;
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)
throwsIOException,ServletException
{
Stringencoding=this.config.getInitParameter("encoding");
StringZXLOVE=this.config.getInitParameter("ZXLOVE");
StringZXSB=this.config.getInitParameter("ZXSB");
StringCZ=this.config.getInitParameter("CZ");
StringQLS=this.config.getInitParameter("QLS");
StringXDH=this.config.getInitParameter("XDH");
StringMA=this.config.getInitParameter("MA");
request.setCharacterEncoding(encoding);
HttpServletRequestrequ=(HttpServletRequest)request;
HttpSessionsession=requ.getSession(true);
StringrequestPath=requ.getServletPath();
if(session.getAttribute("user")==null)
{
request.getRequestDispatcher("/ehtml/nologin.jsp").forward(request,response);
}
elseif((session.getAttribute("user")!=null)&&((requestPath.endsWith(ZXLOVE))||(requestPath.endsWith(ZXSB))||(requestPath.endsWith(CZ))||(requestPath.endsWith(QLS))||(requestPath.endsWith(XDH))||(requestPath.endsWith(MA)))){
Useruser=(User)session.getAttribute("user");
if("A".equals(user.getQx()))
chain.doFilter(request,response);
else
request.getRequestDispatcher("/ehtml/notenoughqx.jsp").forward(request,response);
}
elseif((session.getAttribute("user")!=null)&&(!requestPath.endsWith(ZXLOVE))&&(!requestPath.endsWith(ZXSB))&&(!requestPath.endsWith(CZ))&&(!requestPath.endsWith(QLS))&&(!requestPath.endsWith(XDH))&&(!requestPath.endsWith(MA))){
chain.doFilter(request,response);
}else{
request.getRequestDispatcher("/ehtml/500.jsp").forward(request,response);
}
}
publicvoidinit(FilterConfigconfig)
throwsServletException
{
this.config=config;
}
publicvoiddestroy(){
this.config=null;
}
}
[/code]
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息