为什么网页刷新时,都会执行一次 Request变更属性,再执行一次Request新增属性

为什么网页刷新时,都会执行一次 Request变更属性,再执行一次Request新增属性

为什么网页刷新时,都会执行一次 Request变更属性,再执行一次Request新增属性(控制台输出的第2行,第6行)

控制台输出如下:

HttpServletRequest8已被创建,URI:/listener-interface/HelloServlet

Request变更属性:org.apache.catalina.ASYNC_SUPPORTED:true           

ServletContext变更属性:sc-attr1:sc-attr1-value

ServletContext变更属性:sc-attr2:sc-attr2-value2

HttpSession变更属性:session-attr1:session-attr1-value

Request新增属性:request-attr1:request-attr-value

HttpServletRequest8已被销毁



代码如下:

package com.imooc.listener;


import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class HelloServlet

 */

@WebServlet("/HelloServlet")

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public HelloServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

response.getWriter().println("hello world");

request.getServletContext().setAttribute("sc-attr1", "sc-attr1-value");

request.getServletContext().setAttribute("sc-attr2", "sc-attr2-value2");

request.getSession().setAttribute("session-attr1", "session-attr1-value");

request.setAttribute("request-attr1", "request-attr-value");

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}


}







package com.imooc.listener;


import javax.servlet.ServletContextAttributeEvent;

import javax.servlet.ServletContextAttributeListener;

import javax.servlet.ServletRequestAttributeEvent;

import javax.servlet.ServletRequestAttributeListener;

import javax.servlet.http.HttpSessionAttributeListener;

import javax.servlet.http.HttpSessionBindingEvent;


public class WebAttributeListener

implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {


@Override

public void attributeAdded(ServletRequestAttributeEvent arg0) {

// TODO Auto-generated method stub

System.out.println("Request新增属性:"+arg0.getName()+":"+arg0.getValue());


}


@Override

public void attributeRemoved(ServletRequestAttributeEvent arg0) {

// TODO Auto-generated method stub

System.out.println("Request移除属性:"+arg0.getName()+":"+arg0.getValue());

}


@Override

public void attributeReplaced(ServletRequestAttributeEvent arg0) {

// TODO Auto-generated method stub

System.out.println("Request变更属性:"+arg0.getName()+":"+arg0.getValue());

}


@Override

public void attributeAdded(HttpSessionBindingEvent arg0) {

// TODO Auto-generated method stub

System.out.println("HttpSession新增属性:"+arg0.getName()+":"+arg0.getValue());


}


@Override

public void attributeRemoved(HttpSessionBindingEvent arg0) {

// TODO Auto-generated method stub

System.out.println("HttpSession移除属性:"+arg0.getName()+":"+arg0.getValue());

}


@Override

public void attributeReplaced(HttpSessionBindingEvent arg0) {

// TODO Auto-generated method stub

System.out.println("HttpSession变更属性:"+arg0.getName()+":"+arg0.getValue());

}


@Override

public void attributeAdded(ServletContextAttributeEvent arg0) {

// TODO Auto-generated method stub

System.out.println("ServletContext新增属性:"+arg0.getName()+":"+arg0.getValue());


}


@Override

public void attributeRemoved(ServletContextAttributeEvent arg0) {

// TODO Auto-generated method stub

System.out.println("ServletContext移除属性:"+arg0.getName()+":"+arg0.getValue());

}


@Override

public void attributeReplaced(ServletContextAttributeEvent arg0) {

// TODO Auto-generated method stub

System.out.println("ServletContext变更属性:"+arg0.getName()+":"+arg0.getValue());

}


}


正在回答 回答被采纳积分+1

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

1回答
好帮手慕小脸 2021-04-13 18:29:11

同学你好,因为每次网页刷新,相当于重新访问http://localhost:8080/listener-interface/hello,那么控制台就会打印输出对应的输出语句

祝学习愉快~

  • 提问者 慕沐1462760 #1
    那为什么会输出:Request变更属性:org.apache.catalina.ASYNC_SUPPORTED:true 每次刷新,请求的属性都变更了吗
    2021-04-13 20:33:57
  • 好帮手慕小脸 回复 提问者 慕沐1462760 #2

    同学你好,

    1、因为在项目启动执行后,org.apache.catalina.ASYNC_SUPPORTED这个每次都会更新,所以每次都会出现request属性更新了。

    http://img1.sycdn.imooc.com//climg/6076501809d8aea706110031.jpg

    2、每次刷新没有变更,因为当属性修改时,才触发attributeReplaced()方法,此时属性值没有修改,所以不会变更

    祝学习愉快~

    2021-04-14 10:15:28
  • 提问者 慕沐1462760 #3
    我这里每次刷新页面为什么会输出这个:Request变更属性:org.apache.catalina.ASYNC_SUPPORTED:true 而老师视频中例子演示时并没有输出这个
    2021-04-14 10:27:31
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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