1、场景
最近,负责网站开发,网站前端有三大模块:信息发布、会员服务、机构信息检测,还有个和信息发布类似的归类于其他的模块。
其中会员服务中的子模块报名系统,需要用户登录之后才能进入,其他模块不用登录也可进入。
当未登录的用户点击报名系统的入口模块时,会友好提示用户先登录再浏览的。
2、使用的技术
javaweb的filter拦截器、报错跳转jsp页面。
3、代码
web.xml配置
<!--会员报名系统模块拦截器 --> <filter> <filter-name>MemberFilter</filter-name> <filter-class>website.filter.MemberFilter</filter-class> </filter> <filter-mapping> <filter-name>MemberFilter</filter-name> <url-pattern>/website/member/selectProject.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>MemberFilter</filter-name> <url-pattern>/website/member/notice.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>MemberFilter</filter-name> <url-pattern>/website/member/registrationSystem.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>MemberFilter</filter-name> <url-pattern>/website/member/clickProject.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>MemberFilter</filter-name> <url-pattern>/website/member/registrationInformation.action</url-pattern> </filter-mapping>
因为在报名系统模块在会员服务包下的,个人没有将其单独在会员服务包下划分个报名系统包,而是和其他模块一起放在会员服务包下没做区分,所以在拦截时,需要对涉及到报名系统模块的url请求都做拦截
url带有参数的,在配置中直接写参数前url即可。
MemberFilter.java
package website.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; 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; import org.apache.struts2.ServletActionContext; import core.struts2.Struts2Util; import zou.util.string; public class MemberFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)rep; HttpSession session = request.getSession(); //已登录的就放行,没登录转发到错误提示跳转jsp页面 if( session.getAttribute("onlineUser")!=null){ chain.doFilter(req, rep); return; }else{ request.getRequestDispatcher("/page/error/memberError.jsp").forward(req, rep); }; } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
错误提示跳转jsp页面
<%
String Context = request.getContextPath();
pageContext.setAttribute("Context", Context);
%>
<%@ page contentType="text/html; charset=gbk"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>错误提示</title>
<link rel="stylesheet" type="text/css" href=" ${Context}/css/global.css" />
<script language="javascript" type="text/javascript">
alert("您还未登录,请登录!");
window.top.location = "${Context}/website/index.action";
</script>
</head>
<body>
</body>
</html>
4、补充
filter的执行顺序:如果一个web.xml中有多个filter,那么会按照从上到下一个个执行。在web.xml中,listener监听器比filter早执行,filter拦截器比servlet早执行,不能将三者的顺序调乱。