用jsp+servlet+javabean+oracle实现的网上书店

powerball 2002-04-22 01:53:33
加精
我现在需要实现一个三层架构的网上书店,具体要求如下:jsp是表示层,用于数据反馈;servlet是事物层,用于处理逻辑运算;javabean负责连接数据库与servlet;oracle作为数据库服务器。我现在对于四者之间的相互联系不太清楚,jsp与servlet之间,servlet与bean之间,bean与oracle之间的联系方式请描述清楚。最好附上一个例子,如:图书订购或会员登陆。例子的解释和数据库中表的结构烦请说明。不胜感激,百分相送,略表敬意!!
...全文
450 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ERT 2002-04-23
Listing 3:
ShoppingServlet.java

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import shopping.CD;
public class ShoppingServlet extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session == null) {
res.sendRedirect("http://localhost:8080/error.html");
}
Vector buylist=
(Vector)session.getValue("shopping.shoppingcart");
String action = req.getParameter("action");
if (!action.equals("CHECKOUT")) {
if (action.equals("DELETE")) {
String del = req.getParameter("delindex");
int d = (new Integer(del)).intValue();
buylist.removeElementAt(d);
} else if (action.equals("ADD")) {
//any previous buys of same cd?
boolean match=false;
CD aCD = getCD(req);
if (buylist==null) {
//add first cd to the cart
buylist = new Vector();//first order
buylist.addElement(aCD);
} else { // not first buy
for (int i=0;i<buylist.size();i++) {
CD cd = (CD) buylist.elementAt(i);
if (cd.getAlbum().equals(aCD.getAlbum())) {
cd.setQuantity(cd.getQuantity()+aCD.getQuantity());
buylist.setElementAt(cd,i);
match = true;
} //end of if name matches
} // end of for
if (!match)
buylist.addElement(aCD);
}
}
session.putValue("shopping.shoppingcart", buylist);
String url="/jsp/shopping/EShop.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
} else if (action.equals("CHECKOUT")) {
float total =0;
for (int i=0;i<buylist.size();i++) {
CD anOrder = (CD) buylist.elementAt(i);
float price= anOrder.getPrice();
int qty = anOrder.getQuantity();
total += (price * qty);
}
total += 0.005;
String amount = new Float(total).toString();
int n = amount.indexOf(''.'');
amount = amount.substring(0,n+3);
req.setAttribute("amount",amount);
String url="/jsp/shopping/Checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
}
}
private CD getCD(HttpServletRequest req) {
//imagine if all this was in a scriptlet...ugly, eh?
String myCd = req.getParameter("CD");
String qty = req.getParameter("qty");
StringTokenizer t = new StringTokenizer(myCd,"|");
String album= t.nextToken();
String artist = t.nextToken();
String country = t.nextToken();
String price = t.nextToken();
price = price.replace(''$'','' '').trim();
CD cd = new CD();
cd.setAlbum(album);
cd.setArtist(artist);
cd.setCountry(country);
cd.setPrice((new Float(price)).floatValue());
cd.setQuantity((new Integer(qty)).intValue());
return cd;
}
}


每次用户用Eshop.jsp增加一个商品,页面就请求控制SERVLET。控制SERVLET决定进一步的行动,并处理增加的商品。接着,控制SERVLET实例化一个新的BEAN CD代表选定的商品,并在返回SESSION前更新购物车对象。

Listing 4:
CD.java

package shopping;
public class CD {
String album;
String artist;
String country;
float price;
int quantity;
public CD() {
album="";
artist="";
country="";
price=0;
quantity=0;
}
public void setAlbum(String title) {
album=title;
}
public String getAlbum() {
return album;
}
public void setArtist(String group) {
artist=group;
}
public String getArtist() {
return artist;
}
public void setCountry(String cty) {
country=cty;
}
public String getCountry() {
return country;
}
public void setPrice(float p) {
price=p;
}
public float getPrice() {
return price;
}
public void setQuantity(int q) {
quantity=q;
}
public int getQuantity() {
return quantity;
}
}


注意,我们的SERVLET中具有附加的智能,如果一个物品被重复选择,不会增加新的记录,而是在以前的记录上更新计数。控制SERVLET也响应Cart.jsp中的行为,如修改数量,删除商品,还有结帐。如果结帐,控制通过下述语句转向Checkout.jsp页面(源程序5):

String url="/jsp/shopping/Checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);

Listing 5:
Checkout.jsp

<%@ page session="true"import="java.util.*, shopping.CD"%>
<html>
<head>
<title>Music Without Borders Checkout</title>
</head>
<body bgcolor="#33CCFF">
<font face="Times New Roman,Times"size=+3>
Music Without Borders Checkout
</font>
<hr><p>
<center>
<table border="0"cellpadding="0"width="100%"bgcolor="#FFFFFF">
<tr>
<td><b>ALBUM</b></td>
<td><b>ARTIST</b></td>
<td><b>COUNTRY</b></td>
<td><b>PRICE</b></td>
<td><b>QUANTITY</b></td>
<td></td>
</tr>
<%
Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
String amount = (String) request.getAttribute("amount");
for (int i=0;i<buylist.size();i++) {
CD anOrder = (CD) buylist.elementAt(i);
%>
<tr>
<td><b><%= anOrder.getAlbum() %></b></td>
<td><b><%= anOrder.getArtist() %></b></td>
<td><b><%= anOrder.getCountry() %></b></td>
<td><b><%= anOrder.getPrice() %></b></td>
<td><b><%= anOrder.getQuantity() %></b></td>
</tr>
<%
}
session.invalidate();
%>
<tr>
<td></td>
<td></td>
<td><b>TOTAL</b></td>
<td><b>$<%= amount %></b></td>
<td></td>
</tr>
</table>
<p>
<a href="/examples/jsp/shopping/EShop.jsp">Shop some more!</a>
</center>
</body>
</html>


结帐页面简单地从SESSION中取出购物车,然后显示每个物品和总金额。这里的关键是要结束SESSION,因此在页面中有一个session.invalidate()调用。这一处理有两个原因。首先,如果不结束SESSION,用户的购物车不会被初始化,如果用户要继续购买,车中会保留他已经支付过的商品。另外,如果用户不结帐就离开了,则SESSION会继续占用有效的资源直到过期。过期时间一般是30分钟,在一个大的站点上,这样的情况会很快导致资源耗尽。当然,这是我们不愿看到的。


注意,所有的资源分配在这个例子中是基于SESSION的。所以,你必须确保控制SERVLET不被用户访问,即使是意外的访问也不允许。这可以在控制检查到一个非法访问时用一个简单的重定向错误页面来处理。见源代码6。

Listing 6:
error.html

<html>
<body>
<h1>
Sorry, there was an unrecoverable error!

Please try<a href="/examples/jsp/shopping/EShop.jsp">again</a>.
</h1>
</body>
</html>
回复
ERT 2002-04-23
Listing 2:
Cart.jsp

<%@ page session="true"import="java.util.*, shopping.CD"%>
<%
Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
if (buylist != null&&(buylist.size()>0)) {
%>
<center>
<table border="0"cellpadding="0"width="100%"bgcolor="#FFFFFF">
<tr>
<td><b>ALBUM</b></td>
<td><b>ARTIST</b></td>
<td><b>COUNTRY</b></td>
<td><b>PRICE</b></td>
<td><b>QUANTITY</b></td>
<td></td>
</tr>
<%
for (int index=0;index<buylist.size();index++) {
CD anOrder = (CD) buylist.elementAt(index);
%>
<tr>
<td><b><%= anOrder.getAlbum() %></b></td>
<td><b><%= anOrder.getArtist() %></b></td>
<td><b><%= anOrder.getCountry() %></b></td>
<td><b><%= anOrder.getPrice() %></b></td>
<td><b><%= anOrder.getQuantity() %></b></td>
<td>
<form name="deleteForm"
action="/examples/servlet/ShoppingServlet"
method="POST">
<input type="submit"value="Delete">
<input type="hidden"name="delindex"value=''<%= index %>''>
<input type="hidden"name="action"value="DELETE">
</form>
</td>
</tr>
<% } %>
</table>
<p>
<form name="checkoutForm"
action="/examples/servlet/ShoppingServlet"
method="POST">
<input type="hidden"name="action"value="CHECKOUT">
<input type="submit"name="Checkout"value="Checkout">
</form>
</center>
<% } %>



这里,Cart.jsp按照MVC的模式1处理基于SESSION的购物车的表现。请看Cart.jsp开始处的代码:

<%
Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
if (buylist != null&&(buylist.size()>0)) {
%>

本质上,这段代码从SESSION中取出“购物车”。如果“购物车”为空或者没有被创建,它就什么也不显示。所以,在用户第一次访问应用时,其界面如图:


如果“购物车”不为空,用户选择的商品从车中取出,依次显示在页面上:

<%
for (int index=0;index<buylist.size();index++) {
CD anOrder = (CD) buylist.elementAt(index);
%>

一旦生成一个物品的说明,就使用JSP按照事先设定的模板将其插入静态HTML页面。下图显示了用户选购一些物品后的界面:




需要注意的一个重要的地方是所有关于Eshop.jsp,Cart.jsp的处理有一个控制SERVLET,ShoppingServlet.java,代码在源程序3中:

回复
ERT 2002-04-23
这是一个相关例子:

Listing 1:
EShop.jsp

<%@ page session="true"%>
<html>
<head>
<title>Music Without Borders</title>
</head>
<body bgcolor="#33CCFF">
<font face="Times New Roman,Times"size="+3">
Music Without Borders
</font>
<hr><p>
<center>
<form name="shoppingForm"
action="/examples/servlet/ShoppingServlet"
method="POST">
<b>CD:</b>
<select name=CD>
<option>Yuan | The Guo Brothers | China | $14.95</option>
<option>Drums of Passion | Babatunde Olatunji | Nigeria | $16.95</option>
<option>Kaira | Tounami Diabate| Mali | $16.95</option>
<option>The Lion is Loose | Eliades Ochoa | Cuba | $13.95</option>
<option>Dance the Devil Away | Outback | Australia | $14.95</option>
<option>Record of Changes | Samulnori | Korea | $12.95</option>
<option>Djelika | Tounami Diabate | Mali | $14.95</option>
<option>Rapture | Nusrat Fateh Ali Khan | Pakistan | $12.95</option>
<option>Cesaria Evora | Cesaria Evora | Cape Verde | $16.95</option>
<option>Ibuki | Kodo | Japan | $13.95</option>
</select>
<b>Quantity:</b><input type="text"name="qty"SIZE="3"value=1>
<input type="hidden"name="action"value="ADD">
<input type="submit"name="Submit"value="Add to Cart">
</form>
</center>
<p>
<jsp:include page="Cart.jsp"flush="true"/>
</body>
</html>



回复
powerball 2002-04-22
大部分的jsp书籍上的例子与我的要求不一样,书上的是两层架构,我说的是三层架构,希望大家可以推荐几本有关的参考书。
回复
ronny5 2002-04-22
哇~~~~~~~全部写清楚大概可以出书了吧,前几天看到一本什么java实例教程就是讲这个问题的,现在很多的书就有例子的,而且统一的都是网上书店,你随便找一本就行了,何苦让程序员成为打字员呢
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告