过滤器登录界面无法进入下一个页面

过滤器登录界面无法进入下一个页面

AuthFilter

package com.linmin.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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.linmin.domain.User;

@WebFilter("/AuthFilter")
public class AuthFilter implements Filter {

    public AuthFilter() {
        // TODO Auto-generated constructor stub
    }

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hrequest = (HttpServletRequest)request;
		HttpServletResponse hresponse = (HttpServletResponse)response;
		//获取会话请求中的用户名/密码
		String username = (String)hrequest.getSession().getAttribute("username");
		String password = (String)hrequest.getSession().getAttribute("password");
		System.out.println(username);
		System.out.println(password);
		
		if (username == null || password == null) {
			hresponse.sendRedirect(hrequest.getContextPath() + "/login.jsp");
			return;
		} else {
			chain.doFilter(hrequest, hresponse);
			return;
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

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">
	<script type="text/javascript">
		function changeImg(){
			var codeImg = document.getElementById("codeImg");
			codeImg.src="${pageContext.request.contextPath }/KaptchaServlet?time="+new Date().getTime();
		}
	</script>
</head>
<body>
	<div class="login">
		<div class="header">
			<h1>
                <a href="/login.do">登录</a>
            </h1>
			<p>${ msg }</p>
			<button></button>
		</div>
		<form  method="post" action="${pageContext.request.contextPath }/LoginServlet">
			<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="checkCode" style="width: 150px">
				&nbsp;&nbsp;&nbsp;&nbsp; 
				<img id="codeImg" onclick="changeImg()" src="${pageContext.request.contextPath }/KaptchaServlet" 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>

EncodeFilter

package com.linmin.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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/EncodeFilter")
public class EncodeFilter implements Filter {

	private String encoding;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		encoding=filterConfig.getInitParameter("encoding");
		System.out.println("encoding:"+encoding);
	}
	
    public EncodeFilter() {
        // TODO Auto-generated constructor stub
    }

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		req.setCharacterEncoding(encoding);
		HttpServletResponse res = (HttpServletResponse)response;
		res.setContentType("text/html;charset=" + encoding);
		chain.doFilter(request, response);
	}

}

web.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">
  
  <servlet>
	<servlet-name>KaptchaServlet</servlet-name>
	<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>

	<!-- 修改随机的字母或数字的长度 -->
	<init-param>
		<param-name>kaptcha.textproducer.char.length</param-name>
		<param-value>4</param-value>
	</init-param>

	<!-- 修改字体大小 -->
	<init-param>
		<param-name>kaptcha.textproducer.font.size</param-name>
		<param-value>25</param-value>
	</init-param>
	<init-param>
		<param-name>kaptcha.textproducer.char.space</param-name>
		<param-value>10</param-value>
	</init-param>

	<!-- 修改图片的宽高 -->
	<init-param>
		<param-name>kaptcha.image.width</param-name>
		<param-value>120</param-value>
	</init-param>
	<init-param>
		<param-name>kaptcha.image.height</param-name>
		<param-value>30</param-value>
	</init-param>
</servlet>

	<servlet-mapping>
		<servlet-name>KaptchaServlet</servlet-name>
		<url-pattern>/KaptchaServlet</url-pattern>
	</servlet-mapping>
  
  <display-name>libraryPro</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>
  
   <!-- filter标签用于说明哪个类是过滤器,并在应用启动时自动加载 -->
  <filter>
  	<filter-name>AuthFilter</filter-name>
  	<filter-class>com.linmin.filter.AuthFilter</filter-class>
  </filter>
  
  <filter>
  	<filter-name>EncodeFilter</filter-name>
  	<filter-class>com.linmin.filter.EncodeFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  <!-- filter-mapping标签用于说明过滤器对URL应用的范围,要点有二
	1.filter-name 过滤器名称与filter.filter-name保持一致
	2.url-pattern 说明过滤器作用范围,/* 代表对所有的URL进行过滤
  -->
   <filter-mapping>
  	<filter-name>EncodeFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>AuthFilter</filter-name>
  	<url-pattern>/bookList.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>AuthFilter</filter-name>
  	<url-pattern>/addBook.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>AuthFilter</filter-name>
  	<url-pattern>/categoryList.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>AuthFilter</filter-name>
  	<url-pattern>/addCategory.jsp</url-pattern>
  </filter-mapping>
 
</web-app>

还有怎样可以对css等样式不拦截?

正在回答

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

2回答

同学你好,1、过滤器登录界面无法进入下一个页面,是已经进入到login.jsp页面后,无法再跳转页面吗?  如果是,同学可以 在LoginServlet查看,登录提交后,是否正常跳转到Servlet中,条件不成立造成的页面没有跳转吗

        如果不是,还麻烦同学具体描述一下,过滤器登录界面是不能进入到login.jsp页面中吗?

        2、怎样可以对css等样式不拦截

同学可以获取地址,判断是否是css等文件,如果是,直接放行。

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

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 木木汇成林 提问者 #1
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //完成验证码的校验: //获得session中保存的验证码的信息 String code1 = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); //接收前台表单提交的验证码的信息 String code2 = request.getParameter("checkCode"); if(code2==null || !code2.equalsIgnoreCase(code1)) {//不区分大小写 request.setAttribute("msg", "验证码输入不正确!"); request.getRequestDispatcher("/login.jsp").forward(request,response); return; } //接收数据: String username = request.getParameter("username"); String password = request.getParameter("password"); //封装数据: User user = new User(); user.setUsername(username); user.setPassword(password); //处理数据:完成登录 UserService userService = new UserServiceImpl(); //获取用户列表的集合: List<User> userList = (List<User>) getServletContext().getAttribute("userList"); int flag = userService.login(userList,user); //显示结果: if(flag == 0) { //登录失败 request.setAttribute("msg", "用户或密码错误!"); request.getRequestDispatcher("/login.jsp").forward(request,response); }else { //登录成功 response.sendRedirect(request.getContextPath()+"/bookList.jsp"); } } 可以进入login页面,但是输入正确的用户名、密码和验证码仍跳转到登录页面。 上面代码是我的LoginServlet的get函数
    2020-02-29 17:24:32
  • 木木汇成林 提问者 #2
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hrequest = (HttpServletRequest)request; HttpServletResponse hresponse = (HttpServletResponse)response; //获取会话请求中的用户名/密码 String username = (String)hrequest.getSession().getAttribute("username"); String password = (String)hrequest.getSession().getAttribute("password"); System.out.println(username); System.out.println(password); if (username == null || password == null) { hresponse.sendRedirect(hrequest.getContextPath() + "/login.jsp"); return; } else { chain.doFilter(hrequest, hresponse); return; } } 登录后,过滤器中username和password显示为null
    2020-02-29 17:33:49
  • 好帮手慕小班 回复 提问者 木木汇成林 #3
    同学你好,1、查看同学贴出代码,在login.jsp页面中,使用的post的提交方式,但是在同学贴出的LoginServlet中,将对应的代码写在是get方法中,同学将对应内容放在doPost方法中再来试一下。2、另外,请问同学将用户名存入session会话的步骤在哪里,在登录时,什么时候将用户名与密码存入session中。如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
    2020-02-29 17:49:08
慕神2312244 2020-02-29 15:06:33
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		//排除掉css、图片等
		 String url=req.getRequestURI();
	        if(url.indexOf(".css")>0||url.indexOf(".jpg")>0||url.indexOf(".png")>0) {
	            chain.doFilter(request, response);
	            return;
	        }


  • 补充 HttpServletRequest req =(HttpServletRequest) request;
    2020-02-29 15:07:30
  • 同学的回答很棒,可以这样对css等样式不拦截,继续加油 ,我很看好你哟 祝:学习愉快~
    2020-02-29 15:23:33
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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