关于登录过滤器
才开始做一部分,有一些问题
-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">
<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>
-显示图
老师请问我在登录过滤器里设置了msg2显示用户未登录,但为什么我在login页面什么都没输入点登录却出不来msg2的提示呢
22
收起
正在回答
2回答
同学你好,是的,过滤器不用修改;老师测试代码是可以的,如下
同学将script块放到页面最下方
如果还不可以,同学从以下几方面来排查下
1,查看下登录按钮是否绑定了该事件
2,如果绑定了还不可以建议同学清除下浏览器缓存,如下
祝学习愉快
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星