主jsp
Index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <frameset cols='20%,*'> <frame src='Left.jsp'noresize bordercolor='skyblue'> <frameset rows='20%,*'> <frame src='Login.jsp'noresize bordercolor='skyblue'> <frame src='Main.jsp' name='bottom'> </frameset> </frameset> <body> </body> </html>
Left.jsp
就是左边那个jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 记账 </body> </html>
上边的jsp
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户登陆</title> </head> <body> <html:form action="/login.do" method="post"> <tr> <td>用户名 :</td><td><html:text property="loginUName" style="ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9"/></td> </tr> <tr> <td>密 码 :</td><td><html:password property="loginUPwd" style="ime-mode:disabled;width: 130 px; "/></td> </tr> <tr> <td colspan="2" align="center"><html:submit value="登陆"/></td> </tr> </html:form> </body> </html>
下边的jsp
Main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 记账正文 </body> </html>
登陆成功后的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 登陆成功了 </body> </html>
用户登陆过滤器
OnlineFilter.java
//登陆过滤器
package 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.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import dointerface.ApplicationSource; public class OnlineFilter extends HttpServlet implements Filter { private static final long serialVersionUID = 1L; public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request; HttpServletResponse httpResponse = (HttpServletResponse) response; HttpSession session = httpRequest.getSession(); if(session.getAttribute(ApplicationSource.USER) == null && !httpRequest.getRequestURI().endsWith("/public/Index.jsp") // 这些都是一些需要排除的页面 && !httpRequest.getRequestURI().endsWith("/public/Left.jsp") && !httpRequest.getRequestURI().endsWith("/public/Login.jsp") && !httpRequest.getRequestURI().endsWith("/public/Main.jsp")){ // System.out.println(ApplicationSource.USER); // System.out.println(httpRequest.getContextPath()+httpRequest.getRequestURI().endsWith("/public/Login.jsp")); // System.out.println(httpRequest.getContextPath() + "/public/Index.jsp"); httpResponse.sendRedirect(httpRequest.getContextPath() + "/public/Index.jsp"); } chain.doFilter(request, response); } public void destroy() { } }
web.xml里要注意
<welcome-file-list> <welcome-file>/public/Index.jsp</welcome-file> </welcome-file-list> 欢迎页面要设置好,不然容易造成死循环 <filter> <filter-name>onlineFilter</filter-name> <filter-class>filter.OnlineFilter</filter-class> </filter> <filter-mapping> <filter-name>onlineFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>