您的位置:首页 > 其它

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: