一、 application 与session 两种后端存储的区别
session:会话级存储(不能实现资源共享)
application:应用级存储(共享数据)
二、封装javaBean
能够封装重复的代码
在线人数计算?(实现共享功能,关键字:application)
原理:每次有用户登陆成功进入首页后,在线人数会加1,有用户退出,在线人数会随着减1
在doLogin页面内,过程代码如下:
//计算当前人数
Object obj=application.getAttribute("count");
if(obj==null){
obj=0;
}
Integer count=(Integer)obj;
count++;//人数加1
application.setAttribute("count", count);
在首页,怎么增加(在线人数)的属性,应该长这样:(体现共享)
<li>在线人数<%=application.getAttribute("count") %></li>
三、什么是javaBean
javaBean:泛指 Java对象 (封装的类叫做javaBean,javaBean关键字就是java类)
四、javaBean的优势有哪些
1.解决代码重复编写,减少代码冗余
2.功能区分明确,避免业务逻辑处理与页面显示处理集中在一起造成混乱
3.提高了代码的维护性
五、完整代码
DBHelper类(在util包内)
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.sun.net.httpserver.Authenticator.Result;
import oracle.jdbc.driver.OracleDriver;
public class DBHelper {
//加载驱动
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
//定义连接字符串
private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
public static Connection getCon() {
try {
DriverManager.getConnection(URL, "scott", "123");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//关闭资源
public static void close(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null&&!con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
News类:(在pojo包内)
package pojo;
import java.io.Serializable;
public class News implements Serializable{//实现序列化接口
private int newsId;
private String newsTitle;
private int newsTopic;
private String newsAuthor;
private String newsPublisher;
private String newsContent;
private int newsCount;
private int newsMarker;
public int getNewsId() {
return newsId;
}
public void setNewsId(int newsId) {
this.newsId = newsId;
}
public String getNewsTitle() {
return newsTitle;
}
public void setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
}
public int getNewsTopic() {
return newsTopic;
}
public void setNewsTopic(int newsTopic) {
this.newsTopic = newsTopic;
}
public String getNewsAuthor() {
return newsAuthor;
}
public void setNewsAuthor(String newsAuthor) {
this.newsAuthor = newsAuthor;
}
public String getNewsPublisher() {
return newsPublisher;
}
public void setNewsPublisher(String newsPublisher) {
this.newsPublisher = newsPublisher;
}
public String getNewsContent() {
return newsContent;
}
public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
}
public int getNewsCount() {
return newsCount;
}
public void setNewsCount(int newsCount) {
this.newsCount = newsCount;
}
public int getNewsMarker() {
return newsMarker;
}
public void setNewsMarker(int newsMarker) {
this.newsMarker = newsMarker;
}
@Override
public String toString() {
return "News [newsId=" + newsId + ", newsTitle=" + newsTitle + ", newsTopic=" + newsTopic + ", newsAuthor="
+ newsAuthor + ", newsPublisher=" + newsPublisher + ", newsContent=" + newsContent + ", newsCount="
+ newsCount + ", newsMarker=" + newsMarker + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((newsAuthor == null) ? 0 : newsAuthor.hashCode());
result = prime * result + ((newsContent == null) ? 0 : newsContent.hashCode());
result = prime * result + newsCount;
result = prime * result + newsId;
result = prime * result + newsMarker;
result = prime * result + ((newsPublisher == null) ? 0 : newsPublisher.hashCode());
result = prime * result + ((newsTitle == null) ? 0 : newsTitle.hashCode());
result = prime * result + newsTopic;
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 (newsAuthor == null) {
if (other.newsAuthor != null)
return false;
} else if (!newsAuthor.equals(other.newsAuthor))
return false;
if (newsContent == null) {
if (other.newsContent != null)
return false;
} else if (!newsContent.equals(other.newsContent))
return false;
if (newsCount != other.newsCount)
return false;
if (newsId != other.newsId)
return false;
if (newsMarker != other.newsMarker)
return false;
if (newsPublisher == null) {
if (other.newsPublisher != null)
return false;
} else if (!newsPublisher.equals(other.newsPublisher))
return false;
if (newsTitle == null) {
if (other.newsTitle != null)
return false;
} else if (!newsTitle.equals(other.newsTitle))
return false;
if (newsTopic != other.newsTopic)
return false;
return true;
}
public News() {
// TODO Auto-generated constructor stub
}
public News(int newsId, String newsTitle, int newsTopic, String newsAuthor, String newsPublisher,
String newsContent, int newsCount, int newsMarker) {
super();
this.newsId = newsId;
this.newsTitle = newsTitle;
this.newsTopic = newsTopic;
this.newsAuthor = newsAuthor;
this.newsPublisher = newsPublisher;
this.newsContent = newsContent;
this.newsCount = newsCount;
this.newsMarker = newsMarker;
}
}
NewsDao类:(在dao包类)
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.util.DBHelper;
import pojo.News;
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 t_news02 where news_title like ?");
ps.setString(1, "%"+newName+"%");
rs=ps.executeQuery();
while(rs.next()) {
News news=new News();
//给新闻对象赋值
news.setNewsId(rs.getInt(1));
news.setNewsTitle(rs.getString(2));
news.setNewsTopic(rs.getInt(3));
news.setNewsAuthor(rs.getString(4));
news.setNewsPublisher(rs.getString(5));
news.setNewsContent(rs.getString(6));
news.setNewsCount(rs.getInt(7));
news.setNewsMarker(rs.getInt(8));
//将新闻对象添加到集合中
list.add(news);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return list;
}
}
退出doExit.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//session 的存储时间
//session.setMaxInactiveInterval(60);
//怎么删除session
session.invalidate();
//跳回登录页面
response.sendRedirect("/web07/login.jsp");
//让在线人数减1
Object obj=application.getAttribute("count");
//非空判断
Integer count=(Integer) obj;
count--;
application.setAttribute("count", count);
%>
top.jsp页面:
<%@page language="java" contentType="text/html; charset=UTF-8" %>
<%
//怎么判断一个用户有没有登录
Object username=session.getAttribute("username");
if(username==null){
response.sendRedirect("/web07/login.jsp");
}
%>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContent.request.contentPath }/news/index.jsp"
style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown"> 新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="${pageContext.request.contextPath }/news/add.jsp">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>在线人数<%=application.getAttribute("count") %></a></li>
<li><a><%=session.getAttribute("username") %></a></li>
<li><a href="doExit.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>