servlet中配置登录过滤器
2017-09-25 10:43
543 查看
servlet中配置登录过滤器
实现登录过滤主要依靠实现Filter接口,重写dofilter()方法实现。话不多说,直接上代码
1、在WebContent中建立登录login.jsp和主页面main.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"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="login_action" method="post"> id:<input type="text" name = "id" ><br/> password:<input type="text" name = "password"><br/> <input type="submit" value="提交" > </form> </body> </htAml>
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> main </body> </html>
2、在src中建立filter_demo包,并在包中建立建立servlet类,login.action:
package filter_demo; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.catalina.Session; public class login_action extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub String id = req.getParameter("id"); HttpSession session = req.getSession();//取得session对象 session.setAttribute("id", id); String path = req.getContextPath(); resp.sendRedirect(path+"/main.jsp"); } }
3、在包中建立过滤器类,login_filter.java:
package filter_demo; 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 login_filter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("我是拦截器"); HttpServletRequest servletRequest = (HttpServletRequest) arg0; HttpServletResponse servletResponse = (HttpServletResponse) arg1; HttpSession session = servletRequest.getSession(); String path = servletRequest.getServletPath(); if(path.equals("/login.jsp")||path.equals("/login_action")){ arg2.doFilter(arg0,arg1); System.out.println("通过1"); } else{ String id = (String) session.getAttribute("id"); if(id == null) { System.out.println("拦截"); arg0.getRequestDispatcher("/login.jsp").forward(arg0, arg1); }else { arg2.doFilter(arg0,arg1); } } } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
4、配置web.xml,添加相应的映射:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>filter_demo</display-name> <filter> <filter-name>login_filter</filter-name> <filter-class>filter_demo.login_filter</filter-class> </filter> <filter-mapping> <filter-name>login_filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>login_action</servlet-name> <servlet-class>filter_demo.login_action</servlet-class> </servlet> <servlet-mapping> <servlet-name>login_action</servlet-name> <url-pattern>/login_action</url-pattern> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
以上是代码是web.xml中全部代码,其中有关filter的配置信息如下:
<filter> <filter-name>login_filter</filter-name> <filter-class>filter_demo.login_filter</filter-class> </filter> <filter-mapping> <filter-name>login_filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
到此全部的代码就已经全部写好,启动tomcat,在浏览器中直接输入http://localhost:8080/filter_demo/main.jsp,会发现页面回调到login.jsp而不是main.jsp,这就说明我们的过滤器起作用了。工作机制是在web页面向服务器发送请求之后,请求会先到到过滤器,先触发filter的dofilter函数,对请求的地址和类型和地址进行过滤,选择是否放行或者重定向。若符合要求,选择放行,对浏览器返回main.jsp,若不符合要求则执行重定向方法,对浏览器返回login.jsp
相关文章推荐
- 基于Servlet、JSP、JDBC、MySQL登录模块(包括使用的过滤器和配置)
- 基于Servlet、JSP、JDBC、MySQL的登录模块(含过滤器的使用和配置)
- servlet过滤器配置白名单、黑名单
- servlet+jsp实现过滤器,防止用户未登录访问
- JavaWeb--Servlet过滤器Filter和SpringMVC的HandlerInterceptor(Session和Cookie登录认证)
- 过滤器如何配置(javax.servlet.Filter)?
- Servlet初始配置 监听器和过滤器
- Servlet过滤器创建与配置
- 开发安全应用程序(二)--开发用于进行表单登录处理的 servlet 过滤器
- Servlet过滤器web.xml文件配置
- 通用的用户登录过滤器(SessionFilter)--【web.xml中配置】
- Servlet过滤器,Servlet过滤器创建和配置
- jsp servlet中的过滤器Filter配置总结(转)
- 使用Servlet过滤器实现用户登录验证
- jsp servlet中的过滤器Filter配置总结
- JAVA配置登录过滤器和编码过滤器
- 使用Servlet过滤器实现用户登录验证
- Servlet 过滤器应用实例二 检查登录信息并向客户端返回script
- 实例:用Servlet开发和配置过滤器
- Servlet过滤器,Servlet过滤器创建和配置