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>