注解配置servlet 、filter 观察者设计模式

log4j(监控异常的包):log4j-1.2.16.jar
log4j配置文件放入src文件夹下log4j.properties;log4j2.xml

package com.offcn.test;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class Tset {
    private static Logger log= LogManager.getLogger(Tset.class);
    public static void main(String[] args) {
        /*LogManager日志管理工具,是org.apache.slogging.log4j.LogManager包引入
        * 参数是对哪一个类进行操控
        * Logger导import org.apache.logging.log4j.Logger包;
        * */

       /* 优先级由高到低排列*/
        log.debug("debug");
        log.warn("warn");
        log.info("info");
        log.error("error");

    }

}

用注解的方式配置servlet,(就在servlet里写配置的注解);JDK1.5以上版本支持此配置方式

package com.offcn.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/* jdk 7.0  web  3.0*/


servlet的注解配置
/*loadOnStartup = 1  表示立刻加载*/
@WebServlet(name="userServlet",urlPatterns ="/userServlet",loadOnStartup = 1)
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out =resp.getWriter();
        out.print("hhh");
    }
}

过滤器的注解配置

@WebFilter(filterName ="filter",urlPatterns = "/*")

package com.offcn.filter;

import org.apache.catalina.Session;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName ="filter",urlPatterns = "/*")
public class Filter implements javax.servlet.Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
       //获取session
        HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;

        Session session =(Session) httpServletRequest.getSession();

       //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

观察者设计模式
被观察者

package com.offcn.weibo;

import java.util.Observable;

public class BlogUser extends Observable {
    public  void sendBlog(String title,String context){
        this.setChanged();
        Blog blog=new Blog(title,context);

        this.notifyObservers(blog);//通知观察者,我已经改变了

    }
}

观察者

package com.offcn.weibo;

import java.util.Observable;
import java.util.Observer;

public class FindBlog implements Observer {
    @Override
    public void update(Observable o, Object object) {
        Blog blog=(Blog) object;
        System.out.println("发表的博客标题为:"+blog.getTitle()+"文章的内容为:"+blog.getContext());
    }
}

实例Bean

package com.offcn.weibo;

import java.io.Serializable;

public class Blog implements Serializable {
    private String title;
    private  String context;

    public Blog(String title, String context) {
        this.title = title;
        this.context = context;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }
}

测试类

package com.offcn.weibo;

public class Test1 {
    public static void main(String[] args) {
        BlogUser blogUser=new BlogUser();
        blogUser.addObserver(new FindBlog());//注册:参数是观察者对象
        blogUser.sendBlog("学猫叫","喵喵喵");
    }
}

测试结果:发表的博客标题为:学猫叫文章的内容为:喵喵喵

web项目:
需求分析
数据库设计:ER图

前端:HTML JSP CSS JS
后端(分功能进行开发):(1)实体bean (2)dao层 (3)servlet层

你可能感兴趣的:(注解配置servlet 、filter 观察者设计模式)