Application
简介
和Sesseion,Cookie一样,Application也是一种存储方式但是和Cookie不同的是,Application不是前端存储;和Sesseion不同的是,Sesseion是会话级存储(一个自己电脑里面的内存卡),适合私人信息的存储,而Application是应用级存储,即共享的存储(大家都可以用的内存卡),适合公共信息的存储。
案例
统计在线人数 doLogin.jsp处理登录的界面
<%@page import="web_06.com.util.DBHelper"%> <%@page import="javax.servlet.http.Cookie"%> <%@page import="java.net.CookieHandler"%> <%@page import="oracle.jdbc.driver.OracleDriver" %> <%@ page import="java.sql.*" %> <%@ page import="java.nio.charset.StandardCharsets" %> <%@ page import="java.util.Arrays" %> <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <% request.setCharacterEncoding("utf-8"); String yh = request.getParameter("yh"); String mm = request.getParameter("mm"); //获得连接 Connection con = DBHelper.getCon(); //获得执行对象 PreparedStatement ps = con.prepareStatement("select * from t1_user where t_name=? and t_pwd=?"); ps.setString(1, yh); ps.setString(2, mm); //获得结果集 ResultSet rs = ps.executeQuery(); //判断结果 if (rs.next()) { //要实现存储资源共享,用application //1,将用户名存入到服务器的卡库session中/存到后端 application.setAttribute("uname", yh);//在application中开一个空间储存数据 //计算当前的人数(存储在公共内存卡application中) Object obj=application.getAttribute("count");//从application中取出count if(obj==null){ obj=0; } Integer count=(Integer) obj; count++;//登录成功人数+1 application.setAttribute("count", count);//取出人数 response.sendRedirect("index.jsp"); } else { //重定向 客户端 response.sendRedirect("login.jsp"); } //资源关闭 DBHelper.close(con, ps, rs); %>
doExit.jsp处理退出的界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% //退出功能 //session的存活时间 //服务器一关闭session就会清空数据 //删除session session.invalidate(); //并跳回登录界面 response.sendRedirect("login.jsp"); //退出后,在线人数-1 Object obj=application.getAttribute("count");//从application中拿到countapplication Integer count=(Integer)obj;///转成int count--;//退出后,在线人数-1 application.setAttribute("count", count);//再重新拿到count %>
封装JavaBean
简介
JavaBean泛指java对象
JavaBean实际上是指一种特殊的Java类,它通常用来实现一些比较常用的简单功能,并可以很容易的被重用或者是插入其他应用程序中去。所有遵循“一定编程原则”的Java类都可以被称作JavaBean。
JavaBean是一个遵循特定写法的Java类,是一种Java语言编写的可重用组件,它的方法命名,构造及行为必须符合特定的约定:
- 这个类必须要有一个公共的无参构造函数
- 这个类中的所有属性都要私有化
- 私有化的属性要写public类型的方法(get,set)
- 可以实现Serializable接口
JavaBean的优势
解决代码重复编写,减少代码冗余 功能区分明确,避免业务逻辑处理与页面显示处理集中在一起造成混乱 提高了代码的维护性。
JavaBean的分类
1.封装数据:
- 按照OO原则,属性与数据库表字段相对应
- 属性私有
- 具有public的set/get方法
2.封装业务:
- 具有实现特定功能的方法和方法实现
- 通常与一个封装数据的JavaBean对应
在JSP页面中导入JavaBean
<!-- include包含,可以用它导入别的页面 --> <%@include file="页面路径"%>
JavaBean的使用
//引入JavaBean <%@ page import="org.jbit.news.bean.*" %> //使用JavaBean <% Comment c = new Comment(); c.setCid(1014); c.getCauthor(); %>
封装连接数据库语句
package web_06.com.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import oracle.jdbc.driver.OracleDriver; public class DBHelper { //加载驱动 static { //OracleDriver try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //定义连接字符串 private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl"; //获得连接 public static Connection getCon() { try { return DriverManager.getConnection(URL,"scott","123"); } catch (SQLException e) { e.printStackTrace(); } return null; } //关闭资源 public static void close(Connection con,PreparedStatement ps,ResultSet rs) { try { if (!con.isClosed()) { con.close(); } ps.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } } }
封装新闻对象
package web_06.com.pojo; import java.io.Serializable; public class News implements Serializable{//实现序列化接口,以防之后报错 private int news_Id; private String news_titile; private int news_topic; private String news_author; private String news_publisher; private int news_click; private int news_maker; private String news_content; private String news_cover; public int getNews_Id() { return news_Id; } public void setNews_Id(int news_Id) { this.news_Id = news_Id; } public String getNews_titile() { return news_titile; } public void setNews_titile(String news_titile) { this.news_titile = news_titile; } public int getNews_topic() { return news_topic; } public void setNews_topic(int news_topic) { this.news_topic = news_topic; } public String getNews_author() { return news_author; } public void setNews_author(String news_author) { this.news_author = news_author; } public String getNews_publisher() { return news_publisher; } public void setNews_publisher(String news_publisher) { this.news_publisher = news_publisher; } public int getNews_click() { return news_click; } public void setNews_click(int news_click) { this.news_click = news_click; } public int getNews_maker() { return news_maker; } public void setNews_maker(int news_maker) { this.news_maker = news_maker; } public String getNews_content() { return news_content; } public void setNews_content(String news_content) { this.news_content = news_content; } public String getNews_cover() { return news_cover; } public void setNews_cover(String news_cover) { this.news_cover = news_cover; } @Override public String toString() { return "News [news_Id=" + news_Id + ", news_titile=" + news_titile + ", news_topic=" + news_topic + ", news_author=" + news_author + ", news_publisher=" + news_publisher + ", news_click=" + news_click + ", news_maker=" + news_maker + ", news_content=" + news_content + ", news_cover=" + news_cover + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + news_Id; result = prime * result + ((news_author == null) ? 0 : news_author.hashCode()); result = prime * result + news_click; result = prime * result + ((news_content == null) ? 0 : news_content.hashCode()); result = prime * result + ((news_cover == null) ? 0 : news_cover.hashCode()); result = prime * result + news_maker; result = prime * result + ((news_publisher == null) ? 0 : news_publisher.hashCode()); result = prime * result + ((news_titile == null) ? 0 : news_titile.hashCode()); result = prime * result + news_topic; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; News other = (News) obj; if (news_Id != other.news_Id) return false; if (news_author == null) { if (other.news_author != null) return false; } else if (!news_author.equals(other.news_author)) return false; if (news_click != other.news_click) return false; if (news_content == null) { if (other.news_content != null) return false; } else if (!news_content.equals(other.news_content)) return false; if (news_cover == null) { if (other.news_cover != null) return false; } else if (!news_cover.equals(other.news_cover)) return false; if (news_maker != other.news_maker) return false; if (news_publisher == null) { if (other.news_publisher != null) return false; } else if (!news_publisher.equals(other.news_publisher)) return false; if (news_titile == null) { if (other.news_titile != null) return false; } else if (!news_titile.equals(other.news_titile)) return false; if (news_topic != other.news_topic) return false; return true; } }
封装功能代码
package web_06.com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import web_06.com.pojo.News; import web_06.com.util.DBHelper; public class NewsDao { private Connection con; private PreparedStatement ps; private ResultSet rs; public List<News> queryByName(String newName) { List<News> list=new ArrayList<News>(); try { con=DBHelper.getCon(); ps=con.prepareStatement("select * from t2_news where news_title like ?"); //占位符赋值 ps.setString(1,"%"+newName+"%"); //得到结果集 rs=ps.executeQuery(); while(rs.next()) { News news=new News(); //给新闻对象的属性赋值 news.setNews_Id(rs.getInt(1)); news.setNews_titile(rs.getString(2)); news.setNews_topic(rs.getInt(3)); news.setNews_author(rs.getString(4)); news.setNews_publisher(rs.getString(5)); news.setNews_click(rs.getInt(6)); news.setNews_maker(rs.getInt(7)); news.setNews_content(rs.getString(8)); news.setNews_cover(rs.getString(9)); //将新闻对象添加到集合中 list.add(news); } } catch (Exception e) { e.printStackTrace(); }finally { DBHelper.close(con, ps, rs); } return list; } }
你要尽全力保护你的梦想。那些嘲笑你梦想的人,他们注定失败,他们想把你变成和他们一样。我坚信,只要心中有梦想,我就会与众不同。你也是。