为什么网页刷新时,都会执行一次 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
同学你好,因为每次网页刷新,相当于重新访问http://localhost:8080/listener-interface/hello,那么控制台就会打印输出对应的输出语句
祝学习愉快~
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星