博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
uikit学习
查看>>
java的系统时间,怎么计算从现在到凌晨还剩下多少时间?
查看>>
5次Shift会触发粘滞键的妙用(转)
查看>>
分享做为独立开发者的一些经验(转)
查看>>
运行R 报错R cannot R_TempDir, 继而发现/dev/mapper/VG00-LV01 磁盘空间已满
查看>>
【转】/etc/shadow 口令的加密方法
查看>>
控制程序流程
查看>>
[android] xml文件的序列化
查看>>
python字符串的第一个字符和最后字符
查看>>
《敏捷软件开发》学习笔记 第14章 模板方法模式和策略模式
查看>>
tomcat如何配置启动时自动部署webapps下的war包
查看>>
微软拼音转换工具类ChnCharInfo.dll
查看>>
PGI 遇到的坑
查看>>
重读obj基础教程笔记
查看>>
leetcode------Roman to Integer
查看>>
Java集合详解3:Iterator,fail-fast机制与比较器
查看>>
php还是每天学点好。。
查看>>
涅槃重生之时。。。。。
查看>>
JEECG框架自定义下拉框
查看>>
C++ constexpr类型说明符
查看>>