第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

關(guān)于servlet中doPost()或doGet()方法不執(zhí)行的問(wèn)題?

標(biāo)簽:
Java Html5 JQuery

最近学习Filter过程遇到了这个问题,很是纠结研究了两天总算搞明白了,下面我给大家分享下我的经验吧(有点夸张了):
1、当在过滤器执行过程中如果有报错这种情况下,一般好解决按图索骥怎么都能搞定(一般是路径问题),可是没有报错的情况下对新手来说就很头疼了,init()方法可以正常执行就是不执行doPost()或doGet(),这两天我遇到的情况是这样的,可以正常跳转到servlet但是只是执行了init();dopost根本一点反应都没有,好像就直接跳过一样,最后打断点才找到错误在哪里上代码:
这是我的web配置其中并未在<param-value>LogIn.jsp;failure.jsp;LogInServlet</param-value>中加入Servlet路径这是造成无反应的根本原因。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ImoocFilter</display-name>
  <servlet>
    <servlet-name>LogInServlet</servlet-name>
    <servlet-class>imooc.filter.servlet.LogInServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogInServlet</servlet-name>
    <url-pattern>/LogInServlet</url-pattern>
  </servlet-mapping>
  <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-name>LogIn</filter-name>
    <filter-class>imooc.filter.login.LogIn</filter-class>
    <init-param>
        <param-name>checkjsp</param-name>
        <param-value>LogIn.jsp;failure.jsp;LogInServlet</param-value>
    </init-param>
    <init-param>
            <param-name>charset</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
  </filter>
  <filter-mapping>
    <filter-name>LogIn</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

这是Filter代码(请忽略风骚的测试)

public class LogIn implements Filter {
    FilterConfig config;

    public LogIn() {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("我是快乐的测试");
        HttpServletRequest req=(HttpServletRequest)request;
        HttpServletResponse res=(HttpServletResponse)response;
        HttpSession session=req.getSession();
        String st=config.getInitParameter("checkjsp");
        String charset = config.getInitParameter("charset");
        if(charset==null){
            charset = "UTF-8";
        }
        request.setCharacterEncoding(charset);
        if(st!=null){

            System.out.println("我是快乐的测试1");
            String[] c=st.split(";");
            for(int i=0;i<c.length;i++){
                if(c[i]==null||" ".equals(c[i])) continue;
                if(req.getRequestURL().indexOf(c[i])!=-1){
                    chain.doFilter(request, response);
                    System.out.println(req.getRequestURL()+"结束本方法");
                    return;
                }
            }
            System.out.println("我是快乐的测试34567");
        }
        if(session.getAttribute("username")!=null){
            System.out.println("我是快乐的测试345");
            chain.doFilter(request, response);
        }else{
            System.out.println(request.getParameter("username"));
            System.out.println(session.getAttribute("username"));
            System.out.println("我是快乐的测试347");
            res.sendRedirect("LogIn.jsp");
        }
        System.out.println("我是快乐的测试二");
    }

    public void init(FilterConfig fConfig) throws ServletException {
        config=fConfig;
    }

}

错误直接原因就在代码中,因为web.xml中没有加入Servlet路径,所以在Filter代码运行中涉及到servlet判断中就不可能执行chain.doFilter();没法回到Servlet中,因此看着就像直接跳过一样, 其实根本就没有执行。希望遇到同样问题的同学少走弯路。

點(diǎn)擊查看更多內(nèi)容
5人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消