博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jsp写的简单购书网站
阅读量:6511 次
发布时间:2019-06-24

本文共 9254 字,大约阅读时间需要 30 分钟。

hot3.png

监听器:

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配置如下:
 
NetWorDemo
UI/login.html
com.cjq.servlet.SessionListener

 
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:
 
Insert title here
登录名:
密码:
 
login_confirm.jsp:
 
<%@page import="java.sql.ResultSet"%><%@ page language="java" contentType="text/html; charset=GBK"    pageEncoding="GBK"%>
Insert title here
<% 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); %>
<% }else{ rs.close(); %>
<% }%>
 
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");	}	%>	
<% 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); %>
Insert title here

在线人数:<%= SessionListener.getCount() %>


<% 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);%>

当前在第<%= showPage %>页,共<%= pageCount %>

<% for(int i=(showPage-1)*pageSize+1;i<=rowCount;i++){ %>
<% rs.next(); if(i==pageSize){ break; } } rs.close(); %>
书名 作者 价格(元) 购书
<%=rs.getString("name") %> <%=rs.getString("author") %> <%=rs.getString("price") %> ">选购此书
<% if(showPage!=1){ %>
<% } if(showPage!=pageCount){ %>
<% } %>
到第一页 到上一页 到下一页 到最后一页
 
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");	}%>
<% 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"%>
<% if(session.getAttribute("username")==null){ response.sendRedirect("login.html"); }%>
Insert title here
购书清单

您已选购以下书籍:

<% 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"); %>
<% String sqlBook="select * from book where id="+rsList.getInt("book_id"); ResultSet rsBook = workM.executeQuery(sqlBook); while(rsBook.next()){ %>
<% g_price=g_price+(double)price*b_num; } %>
<% } }catch(Exception e){ out.println(e); } %>
购买数量
书名
单价(元)
总价格(元)
<%=b_num %>
<%=rsBook.getString("name") %>
<% double price = rsBook.getDouble("price"); %>
¥<%=price %>
¥<%=(float)price*b_num %>
货物价格
¥<%=(float)g_price %>
运输费用
¥5.00
总费用
¥<%=(float)g_price+5 %>
继续购书

 

submit_order.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"); }%><% 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);%>
Insert title here
订单确认
订单信息
姓名: <%=session.getAttribute("username") %>
住址: <%=address %>
电话: <%=tel %>

您选购了以下书籍:

<% 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"); %>
<% String sqlBook="select * from book where id="+rsList.getInt("book_id"); ResultSet rsBook=workM.executeQuery(sqlBook); while(rsBook.next()){ %>
<% g_price=price+(double)price*b_num; } %>
<% } }catch(Exception e){ } %>
购买数量
书名
单价(元)
总价格(元)
<%=b_num %>
<%=rsBook.getString("name") %>
<% double price = rsBook.getDouble("price"); %>
¥<%=price %>
¥<%=(double)price*b_num %>
货物价格
¥<%=(float)g_price %>
运输费用
¥5.00
总费用
¥<%=(float)g_price+5 %>

转载于:https://my.oschina.net/u/2552902/blog/543930

你可能感兴趣的文章
[原创]小米手机用户的福音:变相实现小米网盘Mac版客户端
查看>>
如让自己想学不好shell编程都困难?
查看>>
把字符串分隔成多行的多种方法实践
查看>>
SystemCenter2012SP1实践(22)VMM管理VMware的支持特性
查看>>
阿里云部署oracle 11g数据库
查看>>
Lvs+heartbeat高可用高性能web站点的搭建
查看>>
基于淘宝Tengine和Scribe的WEB日志收集方案
查看>>
aix中的几个小技巧与日志
查看>>
MariaDB 10 Slave Crash-Safe需转为GTID复制模式
查看>>
SSD固态硬盘解析和部署注意事项
查看>>
***性测试是一种特殊的信息安全服务
查看>>
话里话外:按单制造ERP(MTO II)诞生的社会效益
查看>>
XenDesktop7-基于SCVMM2012SP1的部署
查看>>
Struts 2的文件上传
查看>>
Hadoop监控分析工具Dr.Elephant
查看>>
在北美闪存峰会上的报告——NVMe SSD数据保护技术
查看>>
DAYDREAM & MIDDLE WARE
查看>>
读书笔记16:组合模式
查看>>
Zabbix 3.4.3之企业微信报警
查看>>
安装配置PgBouncer for PostgreSQL
查看>>