过滤器
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
{
public void destroy()
{
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain
chain)throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
HttpSession session = request.getSession();
if (null == session.getAttribute("uinfo"))
{
request.setAttribute("msg", "您未登录或者离线时间过长,请重新登录");
request.getRequestDispatcher("/ToLoginAction")
.forward(request, response);
}
else
{
chain.doFilter(request, response);
}
}
public void init(FilterConfig arg0) throws ServletException
{
}
}
login.jsp
<input type="hidden" value="${requestScope.msg}" id="msg" />
<script type="text/javascript">
var msg = document.getElementById("msg").value;
if ("" != msg) {
alert(msg);
}
</script>
当然可以在js中直接获取${requestScope.msg}。
web.xml
<filter>
<filter-name>login</filter-name>
<filter-class>com.xy.filter.IsLoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/logined/*</url-pattern>
</filter-mapping>
</filter>
下面是拦截器
package com.xy.interceptor;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.xy.pojo.Manager;
public class AuthInterceptor extends AbstractInterceptor
{
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
HttpSession session = ServletActionContext.getRequest().getSession();
ServletActionContext.getRequest()
.setAttribute("loginMsg", "您未登录或者离线时间过长,请重新登录");
Manager login = (Manager) session.getAttribute("manager");
if (login != null) return invocation.invoke();
else return "mustlogin";
}
}
login.jsp
<script type="text/javascript">
$(function (){
var loginMsg ='<s:property value="#request.loginMsg" escape="false"/>';
if(""!=loginMsg)
{
alert(loginMsg);
}
});
</script>
struts.xml
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<!-- 权限控制拦截器 -->
<interceptor name="login" class="com.xy.interceptor.AuthInterceptor"></interceptor>
<!-- 定义一个包含权限控制的拦截器 -->
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="login"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-action-ref name="tomain"></default-action-ref>
<!-- 定义全局处理结果 -->
<global-results>
<result name="mustlogin">/login.jsp</result>
</global-results>
<action name="tomain" class="com.xy.action.ToMainAction">
<result name="success">/main.jsp</result>
</action>
<action name="toLogin" class="com.xy.action.ToLoginAction">
<result>/login.jsp</result>
</action>
<action name="loginVerify" class="com.xy.action.LoginVerifyAction">
<result name="success" type="chain">tomain</result>
<result name="error">/login.jsp</result>
</action>
<action name="stu_*" class="com.xy.action.StudentAction"
method="{1}">
<interceptor-ref name="mydefault" />
<result name="tomodify">/modify.jsp</result>
<result name="toadd">/add.jsp</result>
<result name="success" type="chain">tomain</result>
</action>
</package>
过滤器和拦截器异同
2022-04-22 01:27:44