关于登录过滤器

关于登录过滤器

才开始做一部分,有一些问题

-Servlet

package com.imooc.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.imooc.domain.User;
import com.imooc.service.UserService;
import com.imooc.service.impl.UserServiceImpl;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 接受数据
String username = request.getParameter("username");
String password = request.getParameter("password");
// 封装数据
User user = new User(username, password);
// 处理数据:完成登录
UserService userService = new UserServiceImpl();
// 获得用户列表的集合
List<User> userList = (List<User>) getServletContext().getAttribute("userList");
User existUser = userService.login(userList, user);
// jsp显示结果
// 调用了login方法下面就是要判断了
if (existUser == null) {
// 登录失败
request.setAttribute("msg", "用户名或密码错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
} else {
// 登录成功
// 将用户信息保存
request.getSession().setAttribute("existUser", existUser);
response.sendRedirect(request.getContextPath() + "/categoryList.jsp");
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}

-登录过滤器AuthFilter

package com.imooc.web.filter;

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 com.imooc.domain.User;

public class AuthFilter implements Filter {

public AuthFilter() {
}

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// 记得这里要强转一下request为它的子类HttpServletrequest,response同理
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

// 获取唯一资源地址
String uri = req.getRequestURI().toString();
// 对后缀进行放行
// 当查找到符合条件的字段时放行,然后返回
if (uri.indexOf(".css") != -1 || uri.indexOf(".jpg") != -1 || uri.indexOf(".png") != -1
|| uri.indexOf("login") != -1 || uri.indexOf(".js") != -1 || uri.indexOf("eckImg") != -1) {
chain.doFilter(request, response);
return;
}

// 对Session中是否有用户进行过滤
// 先从Session域中获取一下
User user = (User) req.getSession().getAttribute("existUser");
// 判断一下是否为空,空字符串是否等于existUser,如果为空则说明还未登录,并转发回登录页面
if (user == null || "".equals(user)) {
// 未登录
request.setAttribute("msg2", "用户未登录");
res.sendRedirect(req.getContextPath() + "/login.jsp");
return;
} else {
// 登录了就放行
chain.doFilter(request, response);
return;
}

}

public void init(FilterConfig fConfig) throws ServletException {
}

}

-xml配置

​<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>test_regist_login</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- 配置监听器 -->
<listener>
<listener-class>com.imooc.web.listener.InitServletContextListener</listener-class>
</listener>

<!-- 配置登录过滤器,/*代表拦截所有请求 -->
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>com.imooc.web.filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

-login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="login">
<div class="header">
<h1>
<a href="/login.do">登录</a>
<p>${ msg }</p>
<p>${ msg2 }</p>
</h1>
<button></button>
</div>
<form action="${pageContext.request.contextPath }/login" method="post">
<div class="name">
<input type="text" id="name" name="username">
<p></p>
</div>
<div class="pwd">
<input type="password" id="pwd" name="password">
<p></p>
</div>
<div class="code">
<input type="text" id="code" name="verifyCode" style="width: 150px">
&nbsp;&nbsp;&nbsp;&nbsp; <img src="code.jpg"
style="width: 150px; height: 42px; vertical-align: middle;">
<p></p>
</div>
<div class="btn-red">
<input type="submit" value="登录" id="login-btn">
</div>
</form>
</div>
</body>
</html>

-显示图

http://img1.sycdn.imooc.com//climg/5fb5be3c096a758a13200852.jpg

老师请问我在登录过滤器里设置了msg2显示用户未登录,但为什么我在login页面什么都没输入点登录却出不来msg2的提示呢

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,是的,过滤器不用修改;老师测试代码是可以的,如下


http://img1.sycdn.imooc.com//climg/5fb62f0e0946be9412380849.jpg

同学将script块放到页面最下方

如果还不可以,同学从以下几方面来排查下

1,查看下登录按钮是否绑定了该事件

http://img1.sycdn.imooc.com//climg/5fb62fa70947ce5806500103.jpg

2,如果绑定了还不可以建议同学清除下浏览器缓存,如下

http://img1.sycdn.imooc.com//climg/5fb62fcc09b0485405320249.jpg

祝学习愉快

好帮手慕阿园 2020-11-19 15:41:42

同学你好,因为在过滤器中判断如果路径中有login等,就会放行然后return,后面的代码就执行不到,所以这里并没有将msg放进去;所以在登录页面上便获取不到

同学可以在页面上使用js获取输入框的值,如果为空进行弹框提示等

参考代码如下

http://img1.sycdn.imooc.com//climg/5fb621750913c6cf04420167.jpg


http://img1.sycdn.imooc.com//climg/5fb62140091a71f504510124.jpg

祝学习愉快

  • 提问者 孫瑪戈 #1
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet" href="css/login.css"> <script type="text/javascript"> function changeImg() { var codeImg = document.getElementById("codeImg"); codeImg.src = "${pageContext.request.contextPath }/KaptchaServlet?time=" + new Date().getTime(); } </script> <script type="text/javascript"> function checkUser() { var name = document.getElementById("name"); var pwd = document.getElementById("pwd"); if (name.value == "" || pwd.value == "") { alert("请输入完整用户信息!"); } } </script> </head> <body> <div class="login"> <div class="header"> <h1> <a href="/login.do">登录</a> <p>${ msg }</p> </h1> <button></button> </div> <form action="${pageContext.request.contextPath }/login.do" method="post"> <div class="name"> <input type="text" id="name" name="username"> <p></p> </div> <div class="pwd"> <input type="password" id="pwd" name="password"> 老师还是没有弹出啊,js块放在上方下方都试过了,过滤器我觉得没有要改的地方啊
    2020-11-19 15:58:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师