過濾器的正確打開方式




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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/javaee http://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>



package com.lg.filter;
 
import com.lg.entity.User;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
public class LimitsFilter
implements Filter
{
private FilterConfig config;
 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
String encoding = this.config.getInitParameter("encoding");
String ZXLOVE = this.config.getInitParameter("ZXLOVE");
String ZXSB = this.config.getInitParameter("ZXSB");
String CZ = this.config.getInitParameter("CZ");
String QLS = this.config.getInitParameter("QLS");
String XDH = this.config.getInitParameter("XDH");
String MA = this.config.getInitParameter("MA");
 
request.setCharacterEncoding(encoding);
HttpServletRequest requ = (HttpServletRequest)request;
HttpSession session = requ.getSession(true);
 
String requestPath = requ.getServletPath();
 
if (session.getAttribute("user") == null)
{
request.getRequestDispatcher("/ehtml/nologin.jsp").forward(request, response);
}
else if ((session.getAttribute("user") != null) && ((requestPath.endsWith(ZXLOVE)) || (requestPath.endsWith(ZXSB)) || (requestPath.endsWith(CZ)) || (requestPath.endsWith(QLS)) || (requestPath.endsWith(XDH)) || (requestPath.endsWith(MA)))) {
User user = (User)session.getAttribute("user");
if ("A".equals(user.getQx()))
chain.doFilter(request, response);
else
request.getRequestDispatcher("/ehtml/notenoughqx.jsp").forward(request, response);
}
else if ((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);
}
}
 
public void init(FilterConfig config)
throws ServletException
{
this.config = config;
}
 
public void destroy() {
this.config = null;
}
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns: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/javaee http://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>



package com.lg.filter;
 
import com.lg.entity.User;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
public class LimitsFilter
implements Filter
{
private FilterConfig config;
 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
String encoding = this.config.getInitParameter("encoding");
String ZXLOVE = this.config.getInitParameter("ZXLOVE");
String ZXSB = this.config.getInitParameter("ZXSB");
String CZ = this.config.getInitParameter("CZ");
String QLS = this.config.getInitParameter("QLS");
String XDH = this.config.getInitParameter("XDH");
String MA = this.config.getInitParameter("MA");
 
request.setCharacterEncoding(encoding);
HttpServletRequest requ = (HttpServletRequest)request;
HttpSession session = requ.getSession(true);
 
String requestPath = requ.getServletPath();
 
if (session.getAttribute("user") == null)
{
request.getRequestDispatcher("/ehtml/nologin.jsp").forward(request, response);
}
else if ((session.getAttribute("user") != null) && ((requestPath.endsWith(ZXLOVE)) || (requestPath.endsWith(ZXSB)) || (requestPath.endsWith(CZ)) || (requestPath.endsWith(QLS)) || (requestPath.endsWith(XDH)) || (requestPath.endsWith(MA)))) {
User user = (User)session.getAttribute("user");
if ("A".equals(user.getQx()))
chain.doFilter(request, response);
else
request.getRequestDispatcher("/ehtml/notenoughqx.jsp").forward(request, response);
}
else if ((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);
}
}
 
public void init(FilterConfig config)
throws ServletException
{
this.config = config;
}
 
public void destroy() {
this.config = null;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章