老师,我的过滤器有问题,请老师看看
package com.imooc.homework.utils; 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; /** * Servlet Filter implementation class loginFilter */ public class AuthFilter implements Filter { /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
package com.imooc.homework.utils; 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; /** * Servlet Filter implementation class EncodeFilter */ public class EncodeFilter implements Filter { private String encoding; /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ 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); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { encoding = fConfig.getInitParameter("encoding"); } }
<?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>homework</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.immoc.homework.utils.InitListener</listener-class> </listener> <filter> <filter-name>AuthFilter</filter-name> <filter-class>com.imooc.homework.utils.AuthFilter</filter-class> </filter> <filter-mapping> <filter-name>AuthFilter</filter-name> <url-pattern>/login.jsp</url-pattern> </filter-mapping> <filter> <filter-name>EncodeFilter</filter-name> <filter-class>com.imooc.homework.utils.EncodeFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodeFilter</filter-name> <url-pattern>/login.jsp</url-pattern> </filter-mapping> </web-app>
页面无法访问这个问题困扰我好几天了,经过我的实验,问题应该处在过滤器上。我新建了一个工程,将所有东西都移过去了唯独除了过滤器的配置信息,一切正常,我把Auth过滤器的配置文件添加到xml文件中,刚刚还能访问的文件就404了。我将Auth过滤器的配置文件删掉,重新加载。按理来说应该恢复原状,可以访问了。但是,竟然我把配置文件删了也不能访问!我把字符集过滤器的配置信息如法炮制,出现了一样的情况。这样就相当于我废了两个页面,因为我删了配置也不能访问,这还是永久性的,太恐怖了吧。我还特地清了缓存,也没用,感觉跟中毒了一样。请老师重点看看我的过滤器拦截范围,我感觉就是过滤器的配置信息出的问题
正在回答
同学你好,同学过滤器的配置是有些问题,编码过滤器应该对所有的请求进行过滤,url-pattern应该是"/"。
另外Auth过滤器的url-pattern只对login.jsp页面进行过滤。login.jsp应该是登录页面,不需要对其进行检查,需要对其他的不包括注册和登录的的路径进行检查。在Auth过滤器中并没有进行任何的判断,直接放行,也就是Auth过滤器没有完成。
从同学的截图上看,同学的login.jsp是在WEB-INF目录下的jsp文件中,WEB-INF目录下的文件,不能通过访问路径直接方法,需要通过转发访问。如果同学输入路径直接访问login.jsp页面,出现404是正常的,同学可以将jsp文件放在webContent目录下,而不是WEB-INF目录下。
祝:学习愉快~
同学你好,同学可以直接拦截所有的路径,url-pattern改为“/*”,然后在过滤器中进行判断,如果包含login,则放行。如果不包括,则进行判断是否登录,如果登录了,则放行,如果没有登录,则跳转到登录页面进行登录。
User user = (User) req.getSession().getAttribute("existUser"); String requestURI = req.getRequestURI(); //不需要过滤的url,包括LoginServlet,css,js,png等。 String[] urls = {"/LoginServlet", ".js",".css", ".ico",".jpg",".png"}; boolean flag = false; for (String str : urls) { if (requestURI.indexOf(str) != -1) { flag = true; break; } } if (flag) { //如果路径中包含登录,css,js等,直接放行 chain.doFilter(request, response); }else{ //如果不包含,则判断是否登录 if (user != null) { //如果登录,则放行 chain.doFilter(request, response); return; }else { // 如果没有登录,跳转到登录页面。 req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return; }
login.jsp页面在WEB-INF目录下,必须转发才能访问。
项目发布后,WebContent即根目录,转发路径中不需要WebContent。
祝:学习愉快~
package com.imooc.homework.utils; 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.homework.entity.User; /** * Servlet Filter implementation class loginFilter */ public class AuthFilter implements Filter { /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse rep = (HttpServletResponse)response; User user = (User)req.getSession().getAttribute("user"); if(user != null) { rep.sendRedirect("/Webcontent/WEB-INF/jsp/login.jsp"); return; } chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
package com.imooc.homework.utils; 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; /** * Servlet Filter implementation class EncodeFilter */ public class EncodeFilter implements Filter { private String encoding; /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; req.setCharacterEncoding(encoding); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { encoding = fConfig.getInitParameter("encoding"); } }
<?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>homework</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.immoc.homework.utils.InitListener</listener-class> </listener> <filter> <filter-name>AuthFilter</filter-name> <filter-class>com.imooc.homework.utils.AuthFilter</filter-class> </filter> <filter-mapping> <filter-name>AuthFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>EncodeFilter</filter-name> <filter-class>com.imooc.homework.utils.EncodeFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodeFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
老师您看这么改对吗
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星