过滤器实栗 登录检测
项目结构
代码如下
登录servlet
package com.ming.servlrt;
import com.ming.factory.DAOFactory;
import com.ming.vo.User;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = "login.jsp";
// 接收userid内容
String userid = req.getParameter("userid");
// 接收userpass内容
String userpadd = req.getParameter("userpass");
// 保存返回信息
List<String> info = new ArrayList<String>();
if(userid == null || "".equals(userid)){
info.add("用户id不能为空");
}
if(userpadd == null || "".equals(userpadd)){
info.add("密码不能为空");
}
// 报错信息为0进行验证
if(info.size() == 0){
User user = new User();
user.setUserid(userid);
user.setPassword(userpadd);
try{
if(DAOFactory.getIuserDAOInstance().findLogin(user)){
info.add("登录成功!" + user.getName());
// 增加session会话
HttpSession session = req.getSession();
session.setAttribute("id", userid);
}else{
info.add("登录失败");
}
}catch (Exception e){
e.printStackTrace();
}
}
// 使用服务器端跳转, 保存数据
req.setAttribute("info", info);
req.getRequestDispatcher(path).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
对于登录的servlet增加了session
过滤器如下
package com.ming.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
// 首页过滤器
public class LoginFile implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// 转型 因为默认的ServletRequest中未提供相关的方法,需要向下转型
HttpServletRequest request = (HttpServletRequest)servletRequest;
// 获取到session
HttpSession session = request.getSession();
// 判断登录
if(session.getAttribute("id") != null){
// 已经登录
filterChain.doFilter(servletRequest, servletResponse);
}else{
// 跳转登录页登录
request.getRequestDispatcher("login.jsp").forward(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
}
}
配置文件如下
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>LoginFile</filter-name>
<filter-class>com.ming.filter.LoginFile</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFile</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.ming.servlrt.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
jsp文件如下
涉及 前端的js验证
采用拦截click事件
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %><%--
Created by IntelliJ IDEA.
User: ming
Date: 19-3-16
Time: 下午11:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>用户登录程序</h2>
<%
List<String> info = (List<String>)request.getAttribute("info");
if(info != null){
Iterator<String> iterator = info.iterator();
while(iterator.hasNext()){
%>
<h4><%=iterator.next()%></h4>
<%
}
}
%>
<form action="loginServlet" method="post">
用户id <input type="text" name="userid" id="uname"/>
密码 <input type="password" name="userpass" id="password"/>
<input type="submit" value="登录" id="submit"/>
<input type="reset" value="重置"/>
</form>
<script>
let submit = document.getElementById("submit");
submit.onclick = (event) => {
let uname = document.getElementById("uname").value;
let password = document.getElementById("password").value;
if(!(/^\w{1,15}/.test(uname))){
alert("用户id为5-15位");
return false;
}
if(!(/^\w{1,15}/.test(password))){
alert("密码必须为5-15位");
return false;
}
return true;
}
</script>
</body>
</html>
在无知的道路上缓步前行