过滤器和拦截器异同

过滤器
package com.xy.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class IsLoginFilter implements Filter
{

<wbr><wbr><wbr>public void destroy()<br><wbr><wbr><wbr>{<br><br><wbr><wbr><wbr>}<br><br><wbr><wbr><wbr>public void doFilter(ServletRequest req, ServletResponse resp, FilterChain<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> chain)throws IOException, ServletException<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr>HttpServletRequest request = (HttpServletRequest) req;<br><wbr><wbr><wbr><wbr><wbr><wbr>HttpServletResponse response = (HttpServletResponse) resp;<br><wbr><wbr><wbr><wbr><wbr><wbr>HttpSession session = request.getSession();<br><wbr><wbr><wbr><wbr><wbr><wbr>if (null == session.getAttribute("uinfo"))<br><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>request.setAttribute("msg", "您未登录或者离线时间过长,请重新登录");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>request.getRequestDispatcher("/ToLoginAction")<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> .forward(request, response);<br><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr>else<br><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>chain.doFilter(request, response);<br><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><br><wbr><wbr><wbr>public void init(FilterConfig arg0) throws ServletException<br><wbr><wbr><wbr>{<br><br><wbr><wbr><wbr>}<br><br> }<br><br><br><span style="font-size:20px"><span style="font-weight:bold">login.jsp</span></span><br><br> &lt;input type="hidden" value="${requestScope.msg}" id="msg" /&gt;<br> &lt;script type="text/javascript"&gt;<br> var msg = document.getElementByIdx_x("msg").value;<br> if ("" != msg) {<br><wbr><wbr><wbr>alert(msg);<br> }<br> &lt;/script&gt;<br> 当然可以在js中直接获取${requestScope.msg}。<br><br><br><span style="font-weight:bold; font-size:20px">web.xml</span><br> &lt;filter&gt;<br><wbr><wbr><wbr>&lt;filter-name&gt;login&lt;/filter-name&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr>&lt;filter-class&gt;com.xy.filter.IsLoginFilter&lt;/filter-class&gt;<br><wbr><wbr><wbr>&lt;/filter&gt;<br><wbr><wbr><wbr>&lt;filter-mapping&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr>&lt;filter-name&gt;login&lt;/filter-name&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr>&lt;url-pattern&gt;/logined/*&lt;/url-pattern&gt;<br><wbr><wbr><wbr>&lt;/filter-mapping&gt;<br> &lt;/filter&gt;<br><br><br><br><br><br><span style="font-size:20px"><br style="font-weight:bold"></span><span style="font-size:20px"><span style="font-weight:bold">下面是拦截器</span></span><br><br> package com.xy.interceptor;<br><br> import javax.servlet.http.HttpSession;<br><br> import org.apache.struts2.ServletActionContext;<br><br> import com.opensymphony.xwork2.ActionInvocation;<br> import com.opensymphony.xwork2.interceptor.AbstractInterceptor;<br> import com.xy.pojo.Manager;<br><br><span style="font-weight:bold">public class AuthInterceptor extends AbstractInterceptor</span><br> {<br><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr>public String intercept(ActionInvocation invocation) throws Exception<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr>HttpSession session = ServletActionContext.getRequest().getSession();<br><wbr><wbr><wbr><wbr><wbr><wbr>ServletActionContext.getRequest()<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> .setAttribute("loginMsg", "您未登录或者离线时间过长,请重新登录");<br><wbr><wbr><wbr><wbr><wbr><wbr>Manager login = (Manager) session.getAttribute("manager");<br><wbr><wbr><wbr><wbr><wbr><wbr>if (login != null) return invocation.invoke();<br><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(拦截器)