81,122
社区成员




<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>
tools.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
public class SetCharacterEncodingFilter implements Filter {
protected FilterConfig filterConfig;
protected String encodingName;
protected boolean enable;
public SetCharacterEncodingFilter() {
encodingName = "UTF-8";
enable = false;
}
// 初始化
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
// 每个请求设置UTF-8
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
HttpServletRequest httprequest = (HttpServletRequest) request;
HttpServletResponse httpresponse = (HttpServletResponse) response;
HttpSession session = httprequest.getSession();
AdminUser user=new AdminUser();
try { //获得在session中所记录的isLogin属性,该属性由登录部分的代码写入
user = (ActingAdminUser) session.getAttribute("user");
if (user!=null) //验证成功,继续处理
{
chain.doFilter(request, response);
} else //验证不成功,让用户登录。
{
String targetURL = httprequest.getContextPath()+"/login.jsp";
httpresponse.sendRedirect(targetURL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void destroy() {
}
}