当前位置:编程学习 > 网站相关 >>

显示用户上次浏览过的商品

显示用户上次浏览过的商品
显示上次浏览商品的实现过程

具体事例代码:
1、
package com.hbsi.domain;
public class Book {
private String id;
private String name;
private String author;
private String descript;
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 getDescript() {
return descript;
}
public void setDescript(String descript) {
this.descript = descript;
}
}
2、
package com.hbsi.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBManager {
/**
 * @param args
 */
static String driver;
static String url;
static String username;
static String password;
static{
InputStream in=DBManager.class.getClassLoader().getResourceAsStream("db.properties");
Properties pro=new Properties();
try {
pro.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver = pro.getProperty("driver");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection con=null;
try {
con=DriverManager.getConnection(url,username,password);
System.out.println("chenggongle");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void closeDB(Connection con,Statement st,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
getConnection();
}
}
3、
package com.hbsi.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hbsi.domain.Book;
import com.hbsi.util.DBManager;
public class BookDao {
public List<Book> getAll() {
Connection conn = null;
PreparedStatement pt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
String sql = "select * from book";
pt = conn.prepareStatement(sql);
rs = pt.executeQuery();
List<Book> list = new ArrayList<Book>();
while (rs.next()) {
Book b = new Book();
b.setId(rs.getString("id"));
b.setName(rs.getString("name"));
b.setAuthor(rs.getString("author"));
b.setDescript(rs.getString("descript"));
list.add(b);
}
return list;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
DBManager.closeDB(conn, pt, rs);
}
}
public Book getBook(String id) {
Connection conn = null;
PreparedStatement pt = null;
ResultSet rs = null;
try {
conn = DBManager.getConnection();
String sql = "select * from book where id=?";
pt = conn.prepareStatement(sql);
pt.setString(1, id);
rs = pt.executeQuery();
//Book b = null;
if(rs.next()){
Book b = new Book();
b.setId(rs.getString("id"));
b.setName(rs.getString("name"));
b.setAuthor(rs.getString("author"));
b.setDescript(rs.getString("descript"));
return b;
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
DBManager.closeDB(conn, pt, rs);
}
}
public static void main(String [] args){
BookDao db = new BookDao();
Book b = db.getBook("2");
System.out.println(b.getName());
}
}
4、
package com.hbsi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hbsi.dao.BookDao;
import com.hbsi.domain.Book;
public class CookieDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//显示所有书名
out.print("本网站的商品有:<br/>");
BookDao bd = new BookDao();
List<Book> list = bd.getAll();
for(Book book:list){
//System.out.println(book.getId());
//out.print("<a href='/BookHistory/servlet/CookieDemo2'?id="+book.getId()+">"+book.getName()+"</a><br/>");
out.print("<a href='/BookHistory/servlet/CookieDemo2?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>");
}
//显示浏览过的书名   bookhistory=1,2,3
out.print("你浏览过的商品:<br/>");
Cookie[] cookies = request.getCookies();
for(int i=0;cookies!=null && i<cookies.length;i++){
if(cookies[i].getName().equals("bookhistory")){
String[] ids = cookies[i].getValue().split(",");
for(String id:ids){
Book book = bd.getBook(id);
out.print(book.getName()+"<br/>");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.hbsi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hbsi.dao.BookDao;
import com.hbsi.domain.Book;
public class CookieDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//显示书的详细信息,根据id获取
String

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,