MVC
MVC: 模型model 视图View 控制器control
mvc 是model view controller ,三层是 UI,BLL ,DAL
三层架构
Model
- 业务处理:业务逻辑(Service)
- 数据持久层:CRUD(Dao[JavaBean])
View
- 展示数据
- 提供连接发起Servlet请求
Controller (Servlet)
- 接收用户请求(req请求参数,session信息)
- 交给业务层处理对应的代码
- 控制视图跳转,重定向
流程:
登录---->接收用户登录请求---->处理用户请求(获取用户登录参数,username,password)---->交给业务层处理登录业务(判断用户名密码是否正确:事务)---->Dao层查询用户名密码是否正确---->数据库
开发中model,entity和pojo的区别
一、Bean
对于Bean而言,我的理解是只要是Java的类的就可以称为一个Bean,更用在Spring上,被Spring管理的对象就可以将其称作为Bean。
它不仅仅可以包括对象的属性以及get,set方法,还可以有具体的业务逻辑。
二、Entity
这个最容易理解,其特点是:数据表对应到实体类的映射
三、Model
Model是MVC中一个概念,可能不和Entity一一对应,因为展示在View层中数据可能是一个Entity的精简,也可能是多个Entity的组合。一句话概括:Model是一个高度优化组合或者精简后的一个用于在View层展示数据的对象。
四、POJO
简单Java对象,貌似没有经常提到或作为类的后缀存在,难道是因为其名字太长?
其特点是:除了属性和get、set方法外不包含具体的业务逻辑方法,这个和上文表述的Model很相像,和Entity区别在于没有和数据表中字段一一对应。
过滤器Filter(重点)
过滤网站数据
java.selvet.Filter
init 和 destroy 实例化 初始化 提供服务 销毁 回收
doFilter
public class xxx implements Filter{
// web服务器启动就初始化了,随时等待过滤对象
public void init(FilterConfig filterConfig) throws ServletException{
// 服务器一启动就设置固有属性,web容器(Tomcat)在启动时,会为每一个web程序创建一个对应的ServletContext对象
filterConfig.getServletContext().serAttribute();
}
public void doFilter(ServletRequest requset, ServletResponse response, FilterChain chain) throws IOExcption, ServletExcpetion{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
// 让请求继续走,如果不写,程序到这里会被拦截停止
chain.doFilter(requset, response);
}
// web服务器启动就初始化了关闭,销毁
public void destroy(){
System.gc();// 垃圾回收
}
}
过滤器链:chain.doFilter(request,response);
public interface FilterChain{
public void doFilter(ServletRequest requset, ServletResponse response) throws IOExcption, ServletExcpetion
}
为过滤器配置web.xml
<filter>
<filter-name>xxx</filter-name>
<filter-class>xx.com.xx.xxx</filter-class>
</filter>
<filter-mapping>
<filter-name>xxx</filter-name>
<!--只要是/servlet请求,都会经过这个过滤器-->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
应用
用户注销完,不能进主页,获取session,进行判断session是否存在
public class SysFilter implements Filter{
public void init(FilterConfig filterConfig) throws ServletException{ }
public void doFilter(ServletRequest requset, ServletResponse response, FilterChain chain) throws IOExcption, ServletExcpetion{
// 因为ServletRequest拿不到session,因此需要转换为HttpServletRequest,这里可以认为是多态的思想,父类无法调用子类的方法,所以就将其强转为子类
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
Object user_session = req.getSesson().getAttribute("USER_SESSION");
// "USER_SESSION"写成在类中的一个常量
// public class Constant{public final static String USER_SESSION = "USER_SESSION";}
// Constant.USER_SESSION
if (user_session == null){
resp.sendRedirect("/xx.jsp") // 用户未用户,则重定向
}
chain.doFilter(requset, response);
}
public void destroy(){
System.gc();// 垃圾回收
}
}
监听器Listener
应用:统计人数,统计Session
public class xxx implements HttpSessionListener{
// 创建,观察者模式
public void sessionCreated(HttpSessionEcent se){
//getSession获取上下文
ServletContext servletContext = se.getSession().getServletContext();
// servletContext.getAtrribute();
}
// 销毁
public void sessionDestroyed(HttpSessionEcent se){}
}