jsp 判断 是否登录,
每个页面判断,代码重复。
可以使用过滤器在web.xml配置。
logfilter.java:
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
UserInfo userinfo = (UserInfo) session.getAttribute("UserInfo");
if(userinfo==null){
response.sendRedirect("../../login1.jsp");
}
else
{
chain.doFilter(servletRequest, servletResponse);
return;
}
}
web.xml:
<filter>
<filter-name>logfilter </filter-name>
<filter-class>com.hime.pub.logfilter </filter-class>
</filter>
<filter-mapping>
<filter-name>logfilter </filter-name>
<url-pattern>/pages/* </url-pattern>
</filter-mapping>
同时解决框架问题:
程序使用了框架:
<frameset rows="80,1*,32" cols="*" frameborder="yes" border="0" framespacing="0" border="1" bordercolor="#000000">
<frame src="./top.jsp" name="topFrame" scrolling="NO" noresize>
<frameset cols="170,*" frameborder="no" border="1" framespacing="2" frameborder="yes">
<frame src="./dtree.jsp" name="leftFrame" scrolling="auto">
<frame src="./blank.jsp" name="main" scrolling="auto">
</frameset>
<frame src="./status.jsp" name="statusFrame" scrolling="NO" marginheight="20" noresize>
</frameset>
基本操作都在"main"中,当登陆超时时,在QueryAction中判断
if (userInfo == null) {
this.setForward(this.getMapping().findForward("LoginFailed"));( <forward name="LoginFailed" path="/login.jsp" />)
this.getRequest().setAttribute("operate_result", "登录超时,请重新登陆!");
return this.getForward();
}
结果在“main”框架中显示login.jsp登录页面,即登陆页面嵌套在原来的框架中。
想要整个框架显示登录页面有一种办法就是,在每个页面判断session,然后再用 <script>window.parent.location.href('../../login.jsp') </script>
来跳转。很麻烦。