課程
/后端開發(fā)
/Java
/Java Web開發(fā)技術(shù)應(yīng)用——過濾器
老師,代碼改到完成之后,可以直接訪問success.jsp頁面了,跟最先的出于安全考慮,直接訪問這個頁面會跳轉(zhuǎn)到Login.jsp界面想違背,這是怎么回事?。???都不能過濾了,求解答!??!
2014-11-17
源自:Java Web開發(fā)技術(shù)應(yīng)用——過濾器 6-1
正在回答
我也遇到這樣的問題 但覺得代碼沒問題
親,根據(jù)你的代碼進行測試,代碼是沒有問題,項目部署之后,訪問success.jsp,可直接跳轉(zhuǎn)到Login.jsp.你看再試試
emily_yq 提問者
百度一下,你就知道
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"??? xmlns="http://java.sun.com/xml/ns/javaee"??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">? <servlet>??? <description>This is the description of my J2EE component</description>??? <display-name>This is the display name of my J2EE component</display-name>??? <servlet-name>first</servlet-name>??? <servlet-class>com.imcoo.first</servlet-class>? </servlet>? <servlet-mapping>??? <servlet-name>first</servlet-name>??? <url-pattern>/servlet/first</url-pattern>? </servlet-mapping>??? <filter>??????? <filter-name>firstfilter</filter-name>??????? <filter-class>com.imcoo.firstfilter</filter-class>??????? <init-param>??????????? <param-name>noLoginPath</param-name>??????????? <param-value>Login.jsp;fail.jsp;first</param-value>??????? </init-param>??? </filter>??? <filter-mapping>??????? <filter-name>firstfilter</filter-name>??????? <url-pattern>/*</url-pattern>??? </filter-mapping>
</web-app>
?
package com.imcoo;
import java.io.IOException;import java.util.Enumeration;
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 javax.servlet.http.HttpSession;
public class firstfilter implements Filter {??? private FilterConfig config;?@Override?public void destroy() {??// TODO Auto-generated method stub
?}
?@Override?public void doFilter(ServletRequest arg0, ServletResponse arg1,???FilterChain arg2) throws IOException, ServletException {????? HttpServletRequest request=(HttpServletRequest) arg0;????? HttpServletResponse response=(HttpServletResponse) arg1;????? HttpSession session=request.getSession();????? String noLoginPath=config.getInitParameter("noLoginPath");????? if(noLoginPath!=null){????? ?String[] array=noLoginPath.split(";");????? ?for(int i=0;i<array.length;i++){????? ??if(array[i]==null||"".equals(array[i])) continue;????? ??if(request.getRequestURI().indexOf(array[i])!=-1){????? ???arg2.doFilter(arg0, arg1);????? ???return;????? ??}????? ??????? ?}????? ?????? }????? if(session.getAttribute("name")!=null){????? ?arg2.doFilter(arg0, arg1);????? }???? ????? else{????? ?response.sendRedirect(request.getContextPath()+"/Login.jsp");????? }????? System.out.println(session.getAttribute("name"));?}
?@Override?public void init(FilterConfig arg0) throws ServletException {??// TODO Auto-generated method stub????????? config=arg0;?}
}
親,把配置文件和過濾器的代碼貼出來看看吧
舉報
本課程主要是從實際生活當(dāng)中的過濾器入手,詳細講解了Java Web過濾器
4 回答有兩個問題,求解答??!
1 回答求解求解求解
1 回答有兩個地方不太明白,請求解答
1 回答求大家解惑
4 回答為什么會進入死循環(huán)。求詳解
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-11-17
我也遇到這樣的問題 但覺得代碼沒問題
2014-11-17
親,根據(jù)你的代碼進行測試,代碼是沒有問題,項目部署之后,訪問success.jsp,可直接跳轉(zhuǎn)到Login.jsp.你看再試試
2014-11-17
百度一下,你就知道
2014-11-17
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
??? xmlns="http://java.sun.com/xml/ns/javaee"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
? <servlet>
??? <description>This is the description of my J2EE component</description>
??? <display-name>This is the display name of my J2EE component</display-name>
??? <servlet-name>first</servlet-name>
??? <servlet-class>com.imcoo.first</servlet-class>
? </servlet>
? <servlet-mapping>
??? <servlet-name>first</servlet-name>
??? <url-pattern>/servlet/first</url-pattern>
? </servlet-mapping>
??? <filter>
??????? <filter-name>firstfilter</filter-name>
??????? <filter-class>com.imcoo.firstfilter</filter-class>
??????? <init-param>
??????????? <param-name>noLoginPath</param-name>
??????????? <param-value>Login.jsp;fail.jsp;first</param-value>
??????? </init-param>
??? </filter>
??? <filter-mapping>
??????? <filter-name>firstfilter</filter-name>
??????? <url-pattern>/*</url-pattern>
??? </filter-mapping>
</web-app>
?
?
package com.imcoo;
import java.io.IOException;
import java.util.Enumeration;
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 javax.servlet.http.HttpSession;
public class firstfilter implements Filter {
??? private FilterConfig config;
?@Override
?public void destroy() {
??// TODO Auto-generated method stub
?}
?@Override
?public void doFilter(ServletRequest arg0, ServletResponse arg1,
???FilterChain arg2) throws IOException, ServletException {
????? HttpServletRequest request=(HttpServletRequest) arg0;
????? HttpServletResponse response=(HttpServletResponse) arg1;
????? HttpSession session=request.getSession();
????? String noLoginPath=config.getInitParameter("noLoginPath");
????? if(noLoginPath!=null){
????? ?String[] array=noLoginPath.split(";");
????? ?for(int i=0;i<array.length;i++){
????? ??if(array[i]==null||"".equals(array[i])) continue;
????? ??if(request.getRequestURI().indexOf(array[i])!=-1){
????? ???arg2.doFilter(arg0, arg1);
????? ???return;
????? ??}
????? ??
????? ?}
????? ?
????? }
????? if(session.getAttribute("name")!=null){
????? ?arg2.doFilter(arg0, arg1);
????? }
????
????? else{
????? ?response.sendRedirect(request.getContextPath()+"/Login.jsp");
????? }
????? System.out.println(session.getAttribute("name"));
?}
?@Override
?public void init(FilterConfig arg0) throws ServletException {
??// TODO Auto-generated method stub
????????? config=arg0;
?}
}
package com.imcoo;
import java.io.IOException;
import java.util.Enumeration;
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 javax.servlet.http.HttpSession;
public class firstfilter implements Filter {
??? private FilterConfig config;
?@Override
?public void destroy() {
??// TODO Auto-generated method stub
?}
?@Override
?public void doFilter(ServletRequest arg0, ServletResponse arg1,
???FilterChain arg2) throws IOException, ServletException {
????? HttpServletRequest request=(HttpServletRequest) arg0;
????? HttpServletResponse response=(HttpServletResponse) arg1;
????? HttpSession session=request.getSession();
????? String noLoginPath=config.getInitParameter("noLoginPath");
????? if(noLoginPath!=null){
????? ?String[] array=noLoginPath.split(";");
????? ?for(int i=0;i<array.length;i++){
????? ??if(array[i]==null||"".equals(array[i])) continue;
????? ??if(request.getRequestURI().indexOf(array[i])!=-1){
????? ???arg2.doFilter(arg0, arg1);
????? ???return;
????? ??}
????? ??
????? ?}
????? ?
????? }
????? if(session.getAttribute("name")!=null){
????? ?arg2.doFilter(arg0, arg1);
????? }
????
????? else{
????? ?response.sendRedirect(request.getContextPath()+"/Login.jsp");
????? }
????? System.out.println(session.getAttribute("name"));
?}
?@Override
?public void init(FilterConfig arg0) throws ServletException {
??// TODO Auto-generated method stub
????????? config=arg0;
?}
}
2014-11-17
親,把配置文件和過濾器的代碼貼出來看看吧