MVC三层架构
- 什么是MVC:Model View Controller 模型、视图、控制器
早些年
- 用户直接访问控制层,控制层就可以直接操作数据库
servlet--CRUD--数据库
弊端:程序十分臃肿,不利于维护
servlet的代码中:处理请求、响应、视图跳转、处理JDBC、处理业务代码
架构:没有什么是加一层解决不了的!如果不够再加一层
程序员调用
|
JDBC
|
Mysql Oracle SqlServer...
MVC三层架构
- Model
- 业务处理:业务逻辑(Service)
- 数据持久层:CRUD(Dao)
- View
- 展示数据
- 提供链接发起Servlet请求(a,form,img…)
-Controller(Servlet) - 接收用户的请求:(req:请求参数、Session信息…)
- 交给业务层处理对应的代码
- 控制视图的跳转
登录-->接受用户的登录请求-->处理用户的请求(获取用户登录的参数,username,password)-->交给业务层处理登录业务(判断用户名密码是否正确:事务)-->Dao层查询用户名和密码是否正确-->数据库
Filter(重点)
- Filter:过滤器,用来过滤网站的数据
- 处理中文乱码
- 登录验证…
- Filter开发步骤:
- 1.导包
- 2.编写过滤器
- 导包不要错
- 实现Filter接口,重写对应的方法即可
public class CharacterEncodingFilter implements Filter {
//初始化:web服务器启动,就已经初始化了,随时等待过滤对象出现!
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter初始化");
}
//Chain:链
/*
1.过滤中的所有代码,在过滤特定请求的时候都会执行
2.必须要让过滤器继续同行
chain.doFilter(request,response);
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-16");
response.setCharacterEncoding("utf-16");
response.setContentType("text/html;charset=utf-16");
System.out.println("CharacterEncodingFilter执行前...");
chain.doFilter(request,response);//让我们的请求继续走,如果不写,程序到这就被拦截停止了
System.out.println("CharacterEncodingFilter执行后...");
}
//销毁
public void destroy() {
System.out.println("CharacterEncodingFilter销毁");
}
}
- 3.在web.xml中配置Filter
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.song.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!--只要是/servlet的任何请求,都会经过这个过滤器-->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
监听器
- 实现一个监听器的接口;(有N种)
- 1.编写一个监听器
- 实现监听器的接口
//统计网站在线人数:统计session
public class OnlineCountListener implements HttpSessionListener {
//创建session监听
//一旦创建一个session就会触发一次这个事件!
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
System.out.println(se.getSession().getId());
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(1);
}else{
int count = onlineCount.intValue();
onlineCount = new Integer(count+1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
//销毁session监听
//一旦销毁一个session就会触发一次这个事件!
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext ctx = se.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(0);
}else{
int count = onlineCount.intValue();
onlineCount = new Integer(count-1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
/*
Session销毁:
1.手动销毁 getSession().invalidate();
2.自动销毁
<session-config>
<session-timeout>1</session-timeout>
</session-config>
*/
}
- 2.web.xml注册监听器
<!--注册监听器-->
<listener>
<listener-class>com.song.listener.OnlineCountListener</listener-class>
</listener>
- 3.看情况是否使用!
过滤器、监听器常见应用
- 监听器:GUI编程中经常使用
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame("中秋节快乐!");//新建一个窗体
Panel panel = new Panel(null);//面板
frame.setLayout(null);//设置窗体的布局
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(0,0,255));
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(0,255,0));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
});
}
}
- 用户登录之后才能进入主页!用户注销后就不能进入主页了!
- 1.用户登录之后,向Session中放入用户的数据
- 2.进入主页的时候要判断用户是否已经登录
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
Object user_session = request.getSession().getAttribute("USER_SESSION");
if (request.getSession().getAttribute("USER_SESSION")==null){
response.sendRedirect("/erroe.jsp");
}
chain.doFilter(request,response);
原文地址:http://www.cnblogs.com/song-hua/p/16908936.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性