0
点赞
收藏
分享

微信扫一扫

JavaBean封装

梦幻之云 2022-04-07 阅读 85

让我们先回顾一下上一篇博客讲的内容

存储:

服务器:session 会话级存储(个人)

客户端:cookie

1.客户端发起请求的时候会把cookie JSESSIONID 自动携带到后端

2.后端获取JSESSIONID,根据id找到对应的session(重新给你session)

3.获取对应的session的数据

今日内容:

application 应用级存储(共享的)

1.统计人数

在登录的方法界面的判断结果里面计算当前人数并且需要进行判断

//计算当前人数(不是存在你的卡里,而是存在整个银行)
Object obj=application.getAttribute("count");
if(obj==null){
obj=0;
}

进行强转和人数加一

Integer count=(Integer)obj;
count++;//人数加一

重新存入

application.setAttribute("count", count);

然后显示到界面中去

 <ul class="nav navbar-nav navbar-right">
<li>在线人数<a><%=application.getAttribute("count") %></a></li>
<li><a><%=application.getAttribute("username") %></a></li>
<li><a href="${pageContext.request.contextPath }">历史记录</a></li>
<li><a href="doExit.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>

注:同时在不同的浏览器上登录人数会相应的增加

封装JavaBean

什么是JavaBean?

Java豆荚,Bean豆子,泛指Java对象,就是每一个Java类

连接数据库的步骤

package com.zking.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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//定义连接字符串
private static final String URL="jdbc:oracle:this:@localhost:1521:orcl";

//获得连接
public static Connection getCon() {
try {
return 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.isClosed()) {
con.close();
}
ps.close();
rs.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

然后将界面中连接数据库的方法进行修改

Connection con = DBHelper.getCon();

资源关闭

DBHelper.close(con, ps, rs);

注意:建议重启一下服务器

pojo包(设置与数据库相同的数值)

package com.zking.pojo;

import java.io.Serializable;

public class News implements Serializable{

private int newId;
private String newsTitle;
private String newsTopic;
private String newsAuthor;
private String newsPublisher;
private String newsContent;
private int newsCount;
private int newsMarker;

//get/set方法
public int getNewId() {
return newId;
}
public void setNewId(int newId) {
this.newId = newId;
}
public String getNewsTitle() {
return newsTitle;
}
public void setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
}
public String getNewsTopic() {
return newsTopic;
}
public void setNewsTopic(String 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 [newId=" + newId + ", 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 + newId;
result = prime * result + ((newsAuthor == null) ? 0 : newsAuthor.hashCode());
result = prime * result + ((newsContent == null) ? 0 : newsContent.hashCode());
result = prime * result + newsCount;
result = prime * result + newsMarker;
result = prime * result + ((newsPublisher == null) ? 0 : newsPublisher.hashCode());
result = prime * result + ((newsTitle == null) ? 0 : newsTitle.hashCode());
result = prime * result + ((newsTopic == null) ? 0 : newsTopic.hashCode());
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 (newId != other.newId)
return false;
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 (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 == null) {
if (other.newsTopic != null)
return false;
} else if (!newsTopic.equals(other.newsTopic))
return false;
return true;
}

//无参有参
public News() {
super();
// TODO Auto-generated constructor stub
}
public News(int newId, String newsTitle, String newsTopic, String newsAuthor, String newsPublisher,
String newsContent, int newsCount, int newsMarker)
{
super();
this.newId = newId;
this.newsTitle = newsTitle;
this.newsTopic = newsTopic;
this.newsAuthor = newsAuthor;
this.newsPublisher = newsPublisher;
this.newsContent = newsContent;
this.newsCount = newsCount;
this.newsMarker = newsMarker;
}


}

实现类(首页的封装)

package com.zking.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 com.zking.pojo.News;
import com.zking.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 T_NEWS where NEWS_TITLE like ?");
ps.setString(1, "%"+newName+"%");
//得到结果集
ResultSet rs = ps.executeQuery();
while(rs.next()) {
News news=new News();
//给新闻对象属性赋值
news.setNewId(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) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return list;
}

}

首页

<%
//点击了表单之后 跳转的是当前这个页面 同时携带一个newName过来(查询的关键字)
String newName=request.getParameter("newName");
if(newName==null){
newName="";//查询所有
}
//破碎重组
newName=new String(newName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
for(News news:new NewsDao().queryByName(newName)){
%>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="${pageContext.request.contextPath}/news/read.jsp?newId=<%=news.getNewId()%>" data-placement="bottom" data-toggle="tooltip" href="" title="<%=news.getNewsTitle()%>">
<%=news.getNewsTitle()%>
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code><%=news.getNewsAuthor()%></code></span>
<span class="glyphicon glyphicon-eye-open"><code><%=news.getNewsCount()%></code></span>
<span class="glyphicon glyphicon-tag"><code><%=news.getNewsMarker()%></code></span>
<span class="glyphicon glyphicon-time"><code><%=news.getNewsPublisher()%></code></span>
</p>
</li>
<%
}

%>

界面重复代码简化

建一个新的jsp文件,把重复的代码放入


<%
//怎么判断一个用户有没有登录
Object username=session.getAttribute("username");
if(username==null){
response.sendRedirect("/web08/login.jsp");
}
%>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContext.request.contextPath}/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="${pageContext.request.contextPath }">历史记录</a></li>
<li><a href="doExit.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>

然后在需要的界面中进行调用

<%--include 包含 --%>
<%@include file="top.jsp" %>

点击退出后在线人数需要减一

//人在线人数减一
Object obj=application.getAttribute("count");
Integer count=(Integer)obj;
count--;
application.setAttribute("countt", count);

下次见咯

举报

相关推荐

0 条评论