深入体验JavaWeb开发内幕——使用Cookie模拟某网站向客户发出提示信息的应用
我们在访问某网站时,例如:购物网站、影视网站等这些网站在你登录时可能会向发出你最近购买的商品或观看过的影片等信息,那么这种效果是如何实现的呢?
具体效果如图:
当用户第一次登录该网站时:
浏览过某资源之后:
当你再次访问该网站时:
你还可以继续浏览那么下次再登录时还会做相似的提示:
相关代码实现:
一个用来封装Book对象的实体类
Book.java
[java]
<span style="font-size:14px;">package net.csdn.bookdomain;
public class Book {
private String id;
private String name;
private String author;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
</span>
一个封装了相应方法的接口
BookDao.java
[java]
<span style="font-size:14px;">package net.csdn.bookdao;
import java.util.List;
import net.csdn.bookdomain.Book;
public inte易做图ce BookDao {
public List<Book> getAllBook();
public Book getBookDescription(String id);
}
</span>
一个封装了获取连接资源、和释放资源的工具类
DBManger.java
[java]
<span style="font-size:14px;">package net.csdn.bookutil;
import java.sql.*;
public class DBManager {
static String url="jdbc:mysql://localhost:3306/book";
static String user="root";
static String password="root";
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection()throws SQLException{
Connection con=DriverManager.getConnection(url, user, password);
return con;
}
public static void dbClose1(Statement st,Connection con){
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void dbClose2(ResultSet rs,Statement st,Connection con){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
dbClose1(st,con);
}
}
</span>
一个对于BookDao的实现类BookDaoImpl.java
[java]
<span style="font-size:14px;">package net.csdn.bookdaoimpl;
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 net.csdn.bookdao.BookDao;
import net.csdn.bookdomain.Book;
import net.csdn.bookutil.DBManager;
public class BookDaoImpl implements BookDao {
public List<Book> getAllBook() {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = DBManager.getConnection();
String sql = "select bookId,bookName,bookAuthor,bookDescription from book";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
List<Book> list = new ArrayList();
补充:Web开发 , 其他 ,