过滤器登录界面无法进入下一个页面
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"> <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等样式不拦截?
32
收起
正在回答
2回答
同学你好,1、过滤器登录界面无法进入下一个页面,是已经进入到login.jsp页面后,无法再跳转页面吗? 如果是,同学可以 在LoginServlet查看,登录提交后,是否正常跳转到Servlet中,条件不成立造成的页面没有跳转吗
如果不是,还麻烦同学具体描述一下,过滤器登录界面是不能进入到login.jsp页面中吗?
2、怎样可以对css等样式不拦截
同学可以获取地址,判断是否是css等文件,如果是,直接放行。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
慕神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; }
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星