监听器:
package com.cjq.servlet;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Application Lifecycle Listener implementation class SessionListener
*
*/
public class SessionListener implements HttpSessionListener {
protected static int count = 0;
/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println(count);
count++;
}
/**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println(count);
if(count>0) count--;
}
public static int getCount(){
return count;
}
}
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>NetWorDemo</display-name>
<welcome-file-list>
<welcome-file>UI/login.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.cjq.servlet.SessionListener</listener-class>
</listener>
</web-app>
Book.java:
package com.cjq.book;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class Book {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:book";
Connection conn = null;
ResultSet rs = null;
public Book(){
try{
Class.forName(sDBDriver);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public ResultSet executeQuery(String sql){
rs = null;
try{
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}catch(SQLException ex){
ex.printStackTrace();
}
return rs;
}
}
login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="UI/login_confirm.jsp">
<tr>
<td>
登录名:<input name="login" size="20"><br>
</td>
</tr><br>
<tr>
<td>
密码:<input type="password" name="password" size="20"><br>
</td>
</tr><br>
<td>
<input type="submit" size="4" value="登录"><br>
</td>
<br>
</form>
</body>
</html>
login_confirm.jsp:
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="users" scope="page" class="com.cjq.book.Book"/>
<%
String name = request.getParameter("login");
name = new String(name.getBytes("UTF-8"));
String pwd = request.getParameter("password");
pwd = new String(pwd.getBytes("UTF-8"));
String sql = "select * from customer where logname='"+name+"' and password='"+pwd+"'";
ResultSet rs = users.executeQuery(sql);
if(rs.next()){
rs.close();
session.putValue("username", name);
%>
<jsp:forward page="browse.jsp"/>
<%
}else{
rs.close();
%>
<jsp:forward page="login.html"/>
<%
}
%>
</body>
</html>
browse.jsp:
<%@page import="com.cjq.servlet.SessionListener"%>
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("login.html");
}
%>
<jsp:useBean id = "mybook" scope="page" class="com.cjq.book.Book"/>
<%
int pageSize = 8;
int showPage = 1;
int rowCount = 0;
int pageCount = 0;
ResultSet rs = mybook.executeQuery("select * from book");
rs.last();
rowCount = rs.getRow();
rs.beforeFirst();
pageCount = ((rowCount%pageSize)==0?(rowCount/pageSize):(rowCount/pageSize)+1);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center><p>在线人数:<%= SessionListener.getCount() %></center>
<br>
<hr>
<br>
<center>
<%
String toPage = request.getParameter("toPage");
if(toPage!=null){
showPage = Integer.parseInt(toPage);
if(showPage>pageCount){
showPage=pageCount;
}else if(showPage<=0){
showPage=1;
}
}
rs.absolute((showPage-1)*pageSize+1);
%>
<h3>
当前在第<font size="4" color="red"><%= showPage %></font>页,共
<font size="4" color="red"><%= pageCount %></font>页
</h3>
<br>
<table border="1" width="100%">
<tr bgcolor="#FFCCCC">
<td width="33%">书名</td>
<td width="33%">作者</td>
<td width="10%">价格(元)</td>
<td width="24%">购书</td>
</tr>
<%
for(int i=(showPage-1)*pageSize+1;i<=rowCount;i++){
%>
<tr bgcolor="#CCFFCC">
<td width="33%"><%=rs.getString("name") %></td>
<td width="33"><%=rs.getString("author") %></td>
<td width="10%"><%=rs.getString("price") %></td>
<td width="24%"><a href="cart.jsp?book_id=<%=rs.getInt("id") %>">选购此书</a></td>
</tr>
<%
rs.next();
if(i==pageSize){
break;
}
}
rs.close();
%>
</table>
<table>
<tr valign="baseline" align="center">
<%
if(showPage!=1){
%>
<td width="150"><a href="browse.jsp?toPage=<%=1 %>">到第一页</a></td>
<td width="150"><a href="browse.jsp?toPage=<%=showPage-1 %>">到上一页</a></td>
<%
}
if(showPage!=pageCount){
%>
<td width="150"><a href="browse.jsp?toPage=<%=showPage+1 %>">到下一页</a></td>
<td width="150"><a href="browse.jsp?toPage=<%=pageCount %>">到最后一页</a></td>
<%
}
%>
<td width="150">
<form action="browse.jsp" method="post">
到
<input type="text" name="toPage" style="height:25px;width:40px" value="<%=showPage %>">页
</form>
</td>
</tr>
</table>
</center>
</body>
</html>
cart.jsp:
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("login.html");
}
%>
<jsp:useBean id="cart" scope="page" class="com.cjq.book.Book"/>
<%
String book_id=request.getParameter("book_id");
String sql = "select id from orders where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"'";
ResultSet rs = cart.executeQuery(sql);
int rowscount=0;
try{
while(rs.next()){
rowscount++;
}
}catch(Exception e){
out.println(e.getMessage());
}
if(rowscount==0){
String sqlBook1 = "select * from book where id="+book_id;
ResultSet rsBook1 = cart.executeQuery(sqlBook1);
while(rsBook1.next()){
String sqlCart="insert into orders (user_name,book_id,book_number,goods_price)"+
"values('"+session.getAttribute("username")+"','"+book_id+"','1',"+rsBook1.getDouble("price")+")";
cart.executeQuery(sqlCart);
}
}else{
String sqlBook2="select * from book where id="+book_id;
ResultSet rsBook2=cart.executeQuery(sqlBook2);
while(rsBook2.next()){
String sqlAdd="update orders set book_number=book_number+1,goods_price="+rsBook2.getDouble("price")+" where book_id="+book_id+" and user_name='"+session.getAttribute("username")+"'";
cart.executeQuery(sqlAdd);
}
}
response.sendRedirect("shopcart.jsp");
%>
shopcart.jsp:
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="workM" scope="page" class="com.cjq.book.Book"/>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("login.html");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<table width="100%" border="0" bgcolor="#CCCCFF">
<tr>
<td>
<div align="center">
<b><font color="FF0000">购书清单</font></b>
</div>
</td>
</tr>
</table>
<form name="form1" method="post" action="order.jsp">
<p>您已选购以下书籍:</p>
<table width="100%" border="0" align="center">
<tr bgcolor="#FFCCCC">
<td width="15%">
<div align="center"><font color="#0000FF">购买数量</font></div>
</td>
<td width="56%">
<div align="center"><font color="#0000FF">书名</font></div>
</td>
<td width="13%">
<div align="center"><font color="#0000FF">单价(元)</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">总价格(元)</font></div>
</td>
</tr>
<%
double g_price,total_price;
g_price=0;
total_price=0;
String sqlList="select * from orders where user_name='"+session.getAttribute("username")+"'";
ResultSet rsList = workM.executeQuery(sqlList);
rsList.beforeFirst();
try{
while(rsList.next()){
int b_num = rsList.getInt("book_number");
%>
<tr bgcolor="#CCFFCC">
<td width="15%">
<div align="center"><font color="#0000FF"><%=b_num %>
<input type="hidden" name="book_number" size="4" value="<%=b_num %>">
</font></div>
</td>
<%
String sqlBook="select * from book where id="+rsList.getInt("book_id");
ResultSet rsBook = workM.executeQuery(sqlBook);
while(rsBook.next()){
%>
<td width="56%">
<div align="center"><font color="#0000FF"><%=rsBook.getString("name") %></font></div>
</td>
<td width="13%">
<%
double price = rsBook.getDouble("price");
%>
<div align="center"><font color="#0000FF">¥<%=price %></font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">¥<%=(float)price*b_num %></font></div>
</td>
<%
g_price=g_price+(double)price*b_num;
}
%>
</tr>
<%
}
}catch(Exception e){
out.println(e);
}
%>
<tr bgcolor="#CCCCFF">
<td colspan="3">
<div align="center"><font color="#0000FF">货物价格</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">¥<%=(float)g_price %></font></div>
</td>
</tr>
<tr>
<td height="18" colspan="3" bgcolor="#CCCC99">
<div align="center"><font color="#0000FF">运输费用</font></div>
</td>
<td height="18" width="16%" bgcolor="#CCCC99">
<div align="center"><font color="#0000FF">¥5.00</font></div>
</td>
</tr>
<tr>
<td height="18" colspan="3" bgcolor="#CCCC99">
<div align="center"><font color="#0000FF">总费用</font></div>
</td>
<td height="18" width="16%" bgcolor="#CCCC99">
<div align="center"><font color="#0000FF">¥<%=(float)g_price+5 %></font></div>
</td>
</tr>
<tr align="center">
<td colspan="4">
<a href="browse.jsp">继续购书</a>
<input type="submit" name="Submit2" value="填写订单"/>
</td>
</tr>
</table>
</form>
<p> </p>
</div>
</body>
</html>
submit_order.jsp:
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="workM" scope="page" class="com.cjq.book.Book"/>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("login.html");
}
%>
<%
String tel = request.getParameter("address");
tel = new String(tel.getBytes("UTF-8"));
String address = request.getParameter("tel");
address = new String(address.getBytes("UTF-8"));
String sqlinsert = "update order set user__address='"+address+"',user_tel='"+tel+"' where user_name='"+session.getAttribute("username")+"'";
workM.executeQuery(sqlinsert);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<b><font color="Fucbsia">订单确认</font></b>
<table width="100%" border="1">
<tr bgcolor="#9999FF">
<td colspan="2">
<div align="center"><b>订单信息</b></div>
</td>
</tr>
<tr>
<td width="12%" bgcolor="#FFCCCC">姓名:</td>
<td width="88%" bgcolor="#CCFFCC"><%=session.getAttribute("username") %></td>
</tr>
<tr>
<td width="12%" bgcolor="#FFCCCC">住址:</td>
<td width="88%" bgcolor="#CCFFCC"><%=address %></td>
</tr>
<tr>
<td width="12%" bgcolor="#FFCCCC">电话:</td>
<td width="88%" bgcolor="#CCFFCC"><%=tel %></td>
</tr>
</table>
<p>您选购了以下书籍:</p>
<table width="100%" border="0" align="center">
<tr bgcolor="#FFCCCC">
<td width="15%">
<div align="center"><font color="#0000FF">购买数量</font></div>
</td>
<td width="56%">
<div align="center"><font color="#0000FF">书名</font></div>
</td>
<td width="13%">
<div align="center"><font color="#0000FF">单价(元)</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">总价格(元)</font></div>
</td>
</tr>
<%
double g_price=0,total_price=0;
String sqlList = "select * from orders where user_name='"+session.getAttribute("username")+"'";
ResultSet rsList = workM.executeQuery(sqlList);
try{
while(rsList.next()){
int b_num = rsList.getInt("book_number");
%>
<tr bgcolor="#CCFFCC">
<td width="15%">
<div align="center">
<font color="#0000FF">
<%=b_num %>
<input type="hidden" name="book_number" size="4" value="<%=b_num %>"/>
</font>
</div>
</td>
<%
String sqlBook="select * from book where id="+rsList.getInt("book_id");
ResultSet rsBook=workM.executeQuery(sqlBook);
while(rsBook.next()){
%>
<td width="56%">
<div align="center"><font color="#0000FF"><%=rsBook.getString("name") %></font></div>
</td>
<td width="13">
<%
double price = rsBook.getDouble("price");
%>
<div align="center"><font color="#0000FF">¥<%=price %></font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">¥<%=(double)price*b_num %></font></div>
</td>
<%
g_price=price+(double)price*b_num;
}
%>
</tr>
<%
}
}catch(Exception e){
}
%>
<tr bgcolor="#CCCCFF">
<td colspan="3">
<div align="center"><font color="#0000FF">货物价格</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">¥<%=(float)g_price %></font></div>
</td>
</tr>
<tr bgcolor="#CCCCFF">
<td colspan="3">
<div align="center"><font color="#0000FF">运输费用</font></div>
</td>
<td width="16%">
<div align="center"><font color="#0000FF">¥5.00</font></div>
</td>
</tr>
<tr bgcolor="#CCCCFF">
<td colspan="3">
<div align="center"><font color="#0000FF">总费用</font></div>
</td>
<td width="16%">
<div align="center"><font color="#FF0000">¥<%=(float)g_price+5 %></font></div>
</td>
</tr>
<tr align="center">
<td colspan="4"></td>
</tr>
</table>
</div>
</body>
</html>
转载于:https://my.oschina.net/u/2552902/blog/543930