美文网首页
javaweb登录过滤器

javaweb登录过滤器

作者: dopaclover | 来源:发表于2017-11-06 11:09 被阅读0次

1.web.xml登录过滤器配置


<filter>
<description>登录过滤器</description>
<filter-name>loginFilter</filter-name>
<filter-class>com.feiwei.filter.LoginFilter</filter-class>
</filter>

 <filter-mapping>  
     <filter-name>loginFilter</filter-name>  
     <url-pattern>/*</url-pattern>  
 </filter-mapping> 

2.LoginFilter登录过滤器实现类

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.HttpSession;
import com.feiwei.util.TipsUtil;

public class LoginFilter implements Filter{
TipsUtil tips=new TipsUtil();
@Override
public void destroy() {}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpSession session = httpRequest.getSession();
        String url=((HttpServletRequest)request).getServletPath();                                         if(url.endsWith("login.jsp")||url.endsWith("login.do")||

url.endsWith(".css")||url.endsWith(".js")||url.endsWith(".jpg")||url.endsWith(".png")) {
chain.doFilter(request, response);
return;
}
if (session.getAttribute("username")== null) {
tips.setSuccess(false);
tips.setMessage("登录超时或没有登录!");
tips.setUrl("/login.jsp");
request.setAttribute("tips", tips);
request.getRequestDispatcher("/tips.jsp").forward(request,response);
} else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}
3.页面跳转提示类

/**

  • 页面跳转提示类
  • @author Administrator

*/
public class TipsUtil {
private boolean success; // 操作是否成功
private String title; //标题
private String message; // 提示消息
private int time =3; // 倒计时效果要几秒钟
private String url; // 重定向地址

public boolean isSuccess() {
    return success;
}
public void setSuccess(boolean success) {
    this.success = success;
    this.title=this.success?"提示":"警告";
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public int getTime() {
    return time;
}
public void setTime(int time) {
    this.time = time;
}
public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

}

4.登录页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8"/>
<title>用户登录</title>
<link rel="stylesheet" href="layui/css/bootstrap.min.css" />
<jsp:include page="common/common.jsp"></jsp:include>
<style type="text/css">
body {
background:url(layui/images/face/bg-login.jpg) #f8f6e9;
}
.mycenter{
margin-top: 100px;
margin-left: auto;
margin-right: auto;
height: 350px;
width:500px;
padding: 5%;
padding-left: 5%;
padding-right: 5%;
}
.mycenter mysign{
width: 440px;
}
.mycenter input,checkbox,button{
margin-top:2%;
margin-left: 10%;
margin-right: 10%;
}
.mycheckbox{
margin-top:10px;
margin-left: 40px;
margin-bottom: 10px;
height: 10px;
}
</style>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" >
function myfun(){
document.getElementById("myform").submit();
}
</script>
</head>
<body>
<form id="myform" action="${pageContext.request.contextPath }/login/login.do" method="post">
<div class="mycenter">
<div class="mysign">
<div class="col-lg-11 text-center text-info">
<h2>请登录</h2>
</div>
<div class="col-lg-10">
<input type="text" class="form-control" name="loginName" placeholder="请输入账户名" required autofocus/>
</div>
<div class="col-lg-10"></div>
<div class="col-lg-10">
<input type="password" class="form-control" name="loginPwd" placeholder="请输入密码" required autofocus/>
</div>
<div class="col-lg-10"></div>
<div class="col-lg-10">
<button onclick="myfun()" type="button" class="btn btn-success col-lg-12">登录</button>
</div>
</div>
</div>
</form>
</body>
</html>

相关文章

  • JavaWeb三大组件之过滤器(Filter)

    title: JavaWeb三大组件之过滤器(Filter)tags: JavaWeb 过滤器categories...

  • javaweb登录过滤器

    1.web.xml登录过滤器配置 登录过滤器...

  • JavaWeb--Filter

    Filter:过滤器,是JavaWeb三大组件(Servlet、Filter、Listener)之一过滤器可以把对...

  • Filter&Listener

    Filter过滤器 概念 Filter表示过滤器,是JavaWeb的三大组件之一,Servlet、Filter、L...

  • day09

    过滤器 1.过滤器介绍 1.1 什么是过滤器 JavaWeb中的过滤器的概念: 对请求和响应进行拦截或者增强的对象...

  • 组件必知必会|那些年我们使用过的轮子—Filter和Proxy

    前言 过滤器Filter是JavaWeb三大组件之一,它与Servlet很相似,过滤器是用来拦截请求的,而不是处理...

  • Filter

    过滤器(Filter) Servlet/Filter/Listener统称为JavaWeb的三大组件.其中Filt...

  • JavaWeb之 Filter

    FIlter作用 过滤器JavaWeb三大组件之一,它与Servlet很相似!不它过滤器是用来拦截请求的,而不是处...

  • 过滤器和拦截器的区别

    摘自知乎 区别:①过滤器是javaweb中的概念,拦截器是spring中的概念。②过滤器是基于回调实现的,拦截器是...

  • JavaWeb使用过滤器实现自动登录功能

    登录前后: 总结一下:1.第一次访问主页:经过过滤器,此时无cookie,无session,则会进入登陆页面,执行...

网友评论

      本文标题:javaweb登录过滤器

      本文链接:https://www.haomeiwen.com/subject/xsakmxtx.html