web学习06

java开发中使用的设计模式

 1.model1

   jsp+javabean(实体类)

 2.model2(模拟mvc开发模式)

   m:model(模型) javabean

   v:view(视图)  jsp

   c:control(控制器) servlet

   

  分层思想

    1.web层

    2.逻辑层

    3.数据层


javaweb中的监听

  

  事件

  事件源

  监听对象

  注册监听

 

  javaweb中可以监听

  httpServletRequest对象

  ServletContext对象

  httpSession对象(监听绑定的对象的变化)

  监听对象的什么?

  1.对象的创建与销毁

  2.域中保存的值的变化

  注意:使用监听器需要跟web.xml中配置

 <!-- 注册监听器 -->
  <listener>
  <listener-class>com.lanou3g.MyServletContextListener</listener-class>
  </listener>

  

监听对象的创建和销毁

1.  ServletContextListener

        public class MyServletContextListener implements ServletContextListener{
	//实现接口中的方法
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("ServletContext初始化");
	}
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("ServletContext销毁");
	}

2.  HttpSessionListener

        public class MyHttpSessionListener implements HttpSessionListener{
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		//HttpSessionEvent session事件对象
		String id = se.getSession().getId();
		System.out.println(id+"创建了");
	}
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		// TODO Auto-generated method stub
		HttpSessionListener.super.sessionDestroyed(se);
	}

3.  ServletRequestListener

监听域对象中属性值得变化

 1. ServletContextAttributeListener

 2. HttpSessionAttributeListener

 3. ServletRequestAttributeListener

public class MyServletRequestAttributeListener implements ServletRequestAttributeListener{
	@Override
	public void attributeAdded(ServletRequestAttributeEvent srae) {
		System.out.println("添加"+srae.getName()+"..."+srae.getValue());
	}
	@Override
	public void attributeRemoved(ServletRequestAttributeEvent srae) {
		System.out.println("删除");
	}
	@Override
	public void attributeReplaced(ServletRequestAttributeEvent srae) {
		System.out.println("替换"+srae.getName()+"..."+srae.getValue());
		
		
	}

监听session中绑定了自定义对象的值得变化

   HttpSessionBindingListener  user

   注意:实现该接口不用去web.xml中配置

   注意:该接口 需要 被绑定的对象的类 来实现该接口

public class User implements HttpSessionBindingListener{
	@Override
	public void valueBound(HttpSessionBindingEvent event) {
	System.out.println("绑定对象");
	System.out.println(event.getName());
	System.out.println(event.getValue());
	}
	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
		System.out.println("解除绑定");
	}
}

过滤器(filter) 解决全站乱码问题

1.实现filter

2.实现方法(声明周期方法 dofilter拦截方法)

3.配置web.xml 配置过滤的路径

 

<!-- 配置要过滤的路径  /*过滤所有路径 -->
 <url-pattern>/*</url-pattern>

 声明周期

随着项目的启动而创建

随着项目的停止而销毁


public class Demo01 implements Filter{
	//声明周期方法
	public Demo01() {
		System.out.println("实例化");
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	System.out.println("初始化");
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		//对请求响应进行编码处理
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		System.out.println("拦截前");
		//放行方法 链 chain
		//将处理好编码格式的响应和请求 向下传递
		chain.doFilter(request, response);
		System.out.println("拦截后");
		
		
	}
	
	@Override
	public void destroy() {
		System.out.println("销毁");
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值