贴个例子:
login.jsp:
<form action="doLogin" method="post"> 用户名:<input type="text" name="userName"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登录"/> <input type="reset" value="重置"/> </form>
创建一个success.jsp用于展示跳转成功。
创建登录跳转Servlet:
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @SuppressWarnings("serial") public class DoLoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userName=req.getParameter("userName"); String password=req.getParameter("password"); System.out.println(userName); if("张三".equals(userName)&&"123".equals(password)){ //校验通过 HttpSession session = req.getSession(); session.setAttribute("userName", userName); resp.sendRedirect("success.jsp"); }else { resp.sendRedirect("login.jsp"); } } }
创建第一个过滤器(解决乱码):
package com.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; public class SetEconding implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest)arg0; //解决乱码问题 req.setCharacterEncoding("UTF-8"); //别忘了放行 arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
创建第一个过滤器(是否登录):
package com.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 LoginFilter implements Filter { private FilterConfig config=null; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest)arg0; HttpServletResponse resp=(HttpServletResponse)arg1; HttpSession session=req.getSession(); String initParam=config.getInitParameter("NotFilter"); String[] notFilterPages=initParam.split(";"); if(notFilterPages!=null){ for(int i=0;i<notFilterPages.length;++i){ //跳过字段为空和空字符串的情况 if(notFilterPages[i]==null||"".equals(notFilterPages[i]))continue; //注意这里是URI路径中的是否含有某个初始值,对于servlet来说,是指配置servlet时指定的url-pattern的值,而不是servlet类名 if(req.getRequestURI().indexOf(notFilterPages[i])!=-1){ arg2.doFilter(arg0, arg1); return; } } } //只要未登录成功,所有的页面跳转请求(免除过滤的页面和servlet处理类除外)都将跳转到登录页面 if(session.getAttribute("userName")!=null){ arg2.doFilter(arg0, arg1); return; }else{ resp.sendRedirect("login.jsp"); return; } } @Override public void init(FilterConfig arg0) throws ServletException { config=arg0; } }
web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>javaEE_filter</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 第1个过滤器 --> <!-- 配置解决乱码过滤器 --> <filter> <filter-name>SetEcondingFilter</filter-name> <filter-class>com.filter.SetEconding</filter-class> </filter> <!-- 配置解决乱码过滤器映射 --> <filter-mapping> <filter-name>SetEcondingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 第2个过滤器 --> <!-- 配置登录过滤器 --> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.filter.LoginFilter</filter-class> <init-param> <param-name>NotFilter</param-name> <param-value>login;doLogin</param-value> </init-param> </filter> <!-- 配置登录过滤器映射 --> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置处理登录的Servlet --> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.servlet.DoLoginServlet</servlet-class> </servlet> <!-- 配置处理登录Servlet的映射 --> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/doLogin</url-pattern> </servlet-mapping> </web-app>