jsp问题 求助

chivas3 2009-03-31 10:48:47
购买功能等实现不了, 登录功能正常。。。。。。希望高手指教。。。。。。。。
booklist.jsp文件 内容如下
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>

<%@ page import="ch10.*" %>


<jsp:useBean id="bookDB" scope="page" class="ch10.BookDB" />

<%
request.setCharacterEncoding("GB2312");
%>

<%
int totalPage=0;
long totalRecord=0;
int pageSize=10;
int pagen=1;
Vector bookList=new Vector();
if(request.getParameter("pagen")!=null)
{
pagen=Integer.parseInt(request.getParameter("pagen"));
}

%>
<html>
<head>
<title>网上书店 选购图书</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">

function openScript(url,name, width, height){
var Win = window.open(url,name,'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=no,status=yes' );
}

</script>
<link rel="stylesheet" href="books.css" type="text/css">
</head>

<body bgcolor="#FFFDFD" text="#000000">
<div align="center">
<table width="750" border="0" cellspacing="1" cellpadding="1">
<tr bgcolor="#A4A4A4">
<td width="120"> </td>
<td width="80" align="center"><a href="./index.jsp"><FONT SIZE="4">首页</FONT></a></td>
<td width="80" align="center"><a href="booklist.jsp"><FONT SIZE="4">在线购物</FONT></a></td>
<td width="100" align="center"><a href="shoppingcar.jsp"><FONT SIZE="4">我的购物车</FONT></a></td>

<td width="80" align="center"><a href="login.jsp"><FONT SIZE="4">用户登录</FONT></a></td>
<td width="80" align="center"><a href="userinfo.jsp"><FONT SIZE="4">用户信息</FONT></a></td>
<td width="80" align="center"><a href="logout.jsp"><FONT SIZE="4">离开网站</FONT></a></td>
<td width="80" align="center"><a href="manage/login.jsp"><FONT SIZE="4">网站管理</FONT></a></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="2" cellpadding="2">

<tr>
<td align="center" width="23%" valign="top">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td> </td>
</tr>
<tr>
<td align="left"><a href="userinfo.jsp"><FONT SIZE="4">用户信息</FONT></a></td>
</tr>
<tr>
<td align="left"><a href="shoppingcar.jsp"><FONT SIZE="4">我的购物车</FONT></a></td>
</tr>
<tr>
<td align="left"><a href="logout.jsp"></FONT>离开网站</FONT></a></td>
</tr>
</table>
</td>
<td align="center" width="77%">
<p> </p>
<p>网上书店所有图书情况:</p>

<table width="100%" border="1" cellspacing="1" cellpadding="1" bordercolor="#CC9966">
<tr align="left">
<td>图书名称</td>
<td>作者</td>

<td>出版社</td>
<td>单价</td>
<td width=110>购买</td>
<td>详细信息</td>
</tr>

<%

String pagem=request.getParameter("pagen");
if(bookDB.execute(pagem))
{
totalRecord=bookDB.getRecordCount();
totalPage=(int)totalRecord/pageSize+1;
bookList = bookDB.getBookList();
Enumeration e= bookList.elements();
while(e.hasMoreElements())
{
BookInfo tempBook = (BookInfo)e.nextElement();

%>
<tr align="left">

<td><%=tempBook.getBookName()%></td>
<td><%=tempBook.getAuthor()%></td>
<td><%=tempBook.getPublisher()%></td>

<td><%=tempBook.getPrice()%></td>
<td align="center"><a href="#" onclick="openScript('purchase.jsp?bookid=<%=tempBook.getId()%>','pur',400,450)">购买</a> </td>
<td align="center"><a href="#" onclick="openScript('showbook.jsp?bookid=<%=tempBook.getId()%>','show',400,450)">详细信息</a> </td>
</tr>
<%
}
}
else
out.print("连接数据库出错!");

%>

</table>
<form name="form1" action="booklist.jsp" method="post">
<table width="90%" border="0" cellspacing="1" cellpadding="1">

<tr>
<td align="left">总计结果为<%= totalRecord %>条,共<%=totalPage%>页 当前页第<%=pagen%>页 
<a href="booklist.jsp">首页</a> 
<% if (pagen>1)
{ %>
<a href="booklist.jsp?page=<%=pagen-1 %>">上一页</a> 
<% } %>
<% if (pagen<=totalPage-1)
{%>
<a href="booklist.jsp?pagen=<%=pagen+1 %>">下一页</a> 
<% } %>
<a href="booklist.jsp?pagen=<%=totalPage %>">未页</a> 
</td>
<td>
<p>跳转到:<input type="text" name="pagen" size="6" value="<%=pagen%>">

<INPUT TYPE="submit" name="submit" value="go">
</p>
</td>
</tr>
</table>

</form>
<br>
</div>
</body>
</html>
...全文
201 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dantin 2009-04-02
  • 打赏
  • 举报
回复
不错,帮顶
sjzxqrj 2009-04-02
  • 打赏
  • 举报
回复
你把这些问题不如仔细想下,或者问下身边的人我觉得比在这里有用,没有人有耐心给你看完这么多的代码的。
承揽网站开发,SEO优化,毕业设计,OA,ERP,空间,域名,企业级管理系统 业务范围:[.net] [java]
rmouse_2005 2009-04-02
  • 打赏
  • 举报
回复
有什么异常说明一下啊
kileli 2009-04-02
  • 打赏
  • 举报
回复
真考验耐性...
doingjava 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 susoft2008 的回复:]
太无聊了!~~~
[/Quote]
顶……
susoft2008 2009-04-01
  • 打赏
  • 举报
回复
太无聊了!~~~
chivas3 2009-04-01
  • 打赏
  • 举报
回复
我也不知道哪里出问题了 呵呵 也许是这段下面这段错了 总之是一点击购买 就出现异常了

while(e.hasMoreElements())
{
BookInfo tempBook = (BookInfo)e.nextElement();

%>
<tr align="left">

<td> <%=tempBook.getBookName()%> </td>
<td> <%=tempBook.getAuthor()%> </td>
<td> <%=tempBook.getPublisher()%> </td>

<td> <%=tempBook.getPrice()%> </td>
<td align="center"> <a href="#" onclick="openScript('purchase.jsp?bookid= <%=tempBook.getId()%>','pur',400,450)">购买 </a>  </td>
<td align="center"> <a href="#" onclick="openScript('showbook.jsp?bookid= <%=tempBook.getId()%>','show',400,450)">详细信息 </a>  </td>
</tr>
<%
}

ben0759 2009-04-01
  • 打赏
  • 举报
回复
你应该把可能出问题的帖出来。。不是全部,没人会看完的。。
guolimin1118 2009-04-01
  • 打赏
  • 举报
回复
后台先试下
看能不能存储
wwj027 2009-04-01
  • 打赏
  • 举报
回复
看看连接数据,数据库 那些有没有问题.,.............
wwj027 2009-04-01
  • 打赏
  • 举报
回复
这么长,叫别人不好找吧....
jourqel 2009-04-01
  • 打赏
  • 举报
回复
是太长了点~看的眼睛有点木~
niuxinlong 2009-04-01
  • 打赏
  • 举报
回复
建议lz先自己找找问题可能出在哪,呵呵太长了这也
chivas3 2009-04-01
  • 打赏
  • 举报
回复
难道真的没人会~~~
erlei20082008 2009-04-01
  • 打赏
  • 举报
回复
我看是没人回了,太长了
chivas3 2009-03-31
  • 打赏
  • 举报
回复
Cart.java 页面如下:

package ch10;
import ch10.*;
import java.util.*;
import java.math.*;
public class Cart
{
private Map itemMap=new HashMap();
private Collection items;


public Map getItemMap()
{
return itemMap;
}

public Collection getItems()

{
return itemMap.values();
}

public void setItems(Collection c)
{
items=c;
}
public int getItemSize()
{
return itemMap.size();
}

public boolean containsItemId(long bookNo)
{
return itemMap.containsKey(bookNo);
}
/*清空购物车*/
public void clear()
{
itemMap.clear();
}

/*向购物车中增加一个item*/
public void addItem(Item item)
{

Item tempItem=(Item)itemMap.get(item.getBookNo());

if(tempItem==null)
{
//item.setAmount(1);
itemMap.put(item.getBookNo(),item);

items=itemMap.values();
}
else
{
int n=item.getAmount()+tempItem.getAmount();
item.setAmount(n);
itemMap.put(item.getBookNo(),item);
items=itemMap.values();
}

}

/*删除购物车中指定的item*/

public Item removeItem(long bookNo)
{
Item tempItem =(Item) itemMap.remove(bookNo);
if(tempItem==null)
return null;
else
{
items=itemMap.values();
return tempItem;
}
}

/*重新设置购物车中的商品数量*/
public void setItemAmount(long bookNo, int amount)
{
Item tempItem = (Item)itemMap.get(bookNo);
tempItem.setAmount(amount);
}



public BigDecimal getTotalPrice()
{
BigDecimal totalPrice=new BigDecimal("0");
Iterator items=itemMap.values().iterator();
BookInfo abook=null;
while(items.hasNext())
{
Item item=(Item)items.next();
BookDB bookdb=new BookDB();
try{

abook= bookdb.getBookInfo(item.getBookNo());
}
catch(Exception e)
{}
BigDecimal price=new BigDecimal(abook.getPrice());
BigDecimal amount=new BigDecimal(item.getAmount());
totalPrice=totalPrice.add(price.multiply(amount));

}
return totalPrice;
}




}
chivas3 2009-03-31
  • 打赏
  • 举报
回复
bookDB.java 内容如下:
package ch10;



import ch10.*;
import java.sql.*;
import java.util.*;

public class BookDB
{
private Connection conn;

private Vector bookList;

private int page = 1; //显示的页码
private int pageSize=10; //每页显示的图书数
private int pageCount =0; //页面总数
private long recordCount =0;

public BookDB()
{
try
{

conn=DataBaseConnection.getConnection();
bookList = new Vector();

}
catch(Exception e)
{
e.printStackTrace();
}
}

public void setBookList()throws Exception
{
Statement stmt =conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from bookinfo");


while(rs.next())
{
BookInfo tempBook=new BookInfo();

tempBook.setId(rs.getLong("id"));
tempBook.setBookName(rs.getString("bookname"));
tempBook.setAuthor(rs.getString("author"));
tempBook.setBookClass(rs.getInt("bookclass"));
tempBook.setIsbn(rs.getString("isbn"));
tempBook.setPrice(rs.getFloat("price"));
tempBook.setPublisher(rs.getString("publisher"));
tempBook.setRegTime(rs.getDate("regtime").toString());
tempBook.setStock(rs.getInt("stock"));
tempBook.setContent(rs.getString("content"));

bookList.add(tempBook);
}

conn.close();


}


public void addBook(BookInfo book)throws Exception
{
PreparedStatement pstmt =conn.prepareStatement("insert into bookinfo(bookname,bookclass,author,publisher,isbn,content,price,stock,regtime)values(?,?,?,?,?,?,?,?,?)");
pstmt.setString(1,book.getBookName());
pstmt.setInt(2,book.getBookClass());
pstmt.setString(3,book.getAuthor());
pstmt.setString(4,book.getPublisher());
pstmt.setString(5,book.getIsbn());
pstmt.setString(6,book.getContent());
pstmt.setFloat(7,book.getPrice());
pstmt.setInt(8,book.getStock());
pstmt.setDate(9,new java.sql.Date(new java.util.Date().getTime()));
pstmt.execute();
conn.close();
}


public void modifyBook(long bookid,int stock)throws Exception
{
Statement stmt =conn.createStatement();
int rs=stmt.executeUpdate("update bookinfo set stock='"+stock+"'where id='"+bookid+"'");
conn.close();
}


public void uploadBook(String isbn,String p)throws Exception
{
Statement stmt =conn.createStatement();
int rs=stmt.executeUpdate("update bookinfo set picture='"+p+"'where isbn='"+isbn+"'");
conn.close();
}


public void deleteBook(long bookId)throws Exception
{
Statement stmt=conn.createStatement();
int rs=stmt.executeUpdate("delete from bookinfo where id='"+bookId+"'");
conn.close();
}


public BookInfo getBookInfo(long bookId)throws Exception
{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from bookinfo where id='"+bookId+"'");
BookInfo book=null;
while(rs.next())
{
book =new BookInfo();
book.setId(rs.getLong("id"));
book.setBookName(rs.getString("bookname"));
book.setBookClass(rs.getInt("bookclass"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setIsbn(rs.getString("isbn"));
book.setContent(rs.getString("content"));
book.setPrice(rs.getFloat("price"));
book.setStock(rs.getInt("stock"));
book.setRegTime(rs.getDate("regtime").toString());
book.setPicture(rs.getString("picture"));
}
//conn.close();
return book;
}


public boolean isEnough(int amount,long bookId)
{


try
{

Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select stock from bookinfo where id='"+bookId+"'");

if(rs.next())
{
if(amount<=rs.getInt(1))
return true;
else
return false;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}


}


/**
* 分页显示图书信息
*/

public boolean execute(String requestPage) throws Exception
{

String sqlStr=null;

try
{
page = Integer.parseInt(requestPage);
}
catch (NumberFormatException e)
{
page = 1;
}



//取出记录数
sqlStr = "select count(*) from bookinfo";
try
{

Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlStr);

if (rs.next())
recordCount = rs.getInt(1);
rs.close();

}
catch (SQLException e)
{
e.printStackTrace();
return false;
}





//设定总页面数量
if (recordCount < 1)
pageCount = 0;
else
pageCount = (int)(recordCount - 1) / pageSize + 1;

//检查查看的页面数是否在范围内
if (page < 1)
page = 1;
else if (page > pageCount)
page = pageCount;





sqlStr="select * from bookinfo";



if (page == 1)
{
sqlStr = sqlStr + " order by id limit 0,"+pageSize;
}
else
{
sqlStr = sqlStr + " order by id limit "+(pageSize*(page-1))+","+pageSize;

}



try
{

Statement stmt=conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlStr);

while (rs.next())
{

BookInfo book = new BookInfo();

book.setId(rs.getLong("id"));
book.setBookName(rs.getString("bookname"));
book.setBookClass(rs.getInt("bookclass"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setIsbn(rs.getString("isbn"));
book.setContent(rs.getString("content"));
book.setPrice(rs.getFloat("price"));
book.setStock(rs.getInt("stock"));
book.setRegTime(rs.getDate("regtime").toString());

bookList.add(book);
}
rs.close();
return true;
}

catch (SQLException e)
{


return false;
}



}


public Vector getBookList() throws Exception
{
return bookList;
}

public int getPage()
{ //显示的页码
return page;
}
public void setPage(int newpage)
{
page = newpage;
}

public int getPageSize()
{ //每页显示的图书数
return pageSize;
}
public void setPageSize(int newpsize)
{
pageSize = newpsize;
}

public int getPageCount()
{ //页面总数
return pageCount;
}
public void setPageCount(int newPageCount)
{
pageCount =newPageCount;
}

public long getRecordCount()
{
return recordCount;
}
public void setRecordCount(long newrcount)
{
recordCount= newrcount;
}
}

chivas3 2009-03-31
  • 打赏
  • 举报
回复
purchase.jsp页面 内容如下
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="ch10.*" %>

<jsp:useBean id="myCar" scope="session" class="ch10.Cart" />
<%
request.setCharacterEncoding("gb2312");
String username = (String)session.getAttribute("username");
if ( username == null || username.equals("") )
{
response.sendRedirect("login.jsp?msg=nologin");
}

String message = "";
String submits = request.getParameter("submit");
String bookid=request.getParameter("bookid");
long id=0;
BookInfo book=null;
int amount=1;

if (request.getParameter("bookid")==null || request.getParameter("bookid").equals(""))
{
message = "你要购买的图书不存在!";
}
else {
try {
id = Long.parseLong(bookid);

BookDB bookDB=new BookDB();
book=bookDB.getBookInfo(id);



if (book==null)
{
message = "你要购买的图书不存在!";
}
}
catch (Exception e)
{
message = "你要购买的图书不存在!";
}
}


if (submits!=null && !submits.equals(""))
{
Item item=new Item();
String indentNo = username.substring(0,2)+new java.util.Date().toString().substring(0,10);
item.setIndentNo(indentNo);
item.setBookNo(id);

//item.setBook(book);
amount=Integer.parseInt(request.getParameter("amount"));
item.setAmount(amount);

//out.print(amount);
//out.print(item.getAmount());

myCar.addItem(item);

message = "你要的图书已经放入你的购物车中!谢谢";
/*if (myCar.addnew(request)){
mesg = "你要的图书已经放入你的购物车中!谢谢";
} else if (shop.getIsEmpty()){
mesg = "库存图书数量不足!只剩"+shop.getLeaveBook()+"本";
} else {
mesg = "暂时不能购买!";
}
}else {
*/
}
%>
<html>
<head>
<title>网上书店 购买图书</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">

function openScript(url,name, width, height){
var Win = window.open(url,name,'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=no,status=yes' );
}

function check()
{
if (document.form1.amount.value<1){
alert("你的购买数量有问题");
document.form1.amount.focus();
return false;
}
return true;
}

</script>
<link rel="stylesheet" href="books.css" type="text/css">
</head>

<body bgcolor="#FFFDFD" text="#000000" onload="javascript:window.focus();">
<div align="center">
<p>网上书店欢迎你<font color="#CC0066">选购图书</font>!</p>
<% if(!message.equals(""))
{
out.println(message);
}
%>
<table width="90%" border="0" cellspacing="2" cellpadding="1">
<form name="form1" method="post" action="purchase.jsp">
<tr>
<td align="center">图书名:<%= book.getBookName() %></td>
</tr>
<tr align="center">
<td>你想要的数量:
<input type="text" name="amount" maxlength="4" size="3" value="<%=amount%>"> 本</td>
</tr>
<tr align="center">
<td>
<input type="hidden" name="bookid" value="<%=id %>">
<input type="submit" name="submit" value="购 买" onclick="return(check());">
<input type="reset" name="reset" value="取 消">
</td>
</tr>
<tr align="center">
<td><a href="#" onclick="openScript('showbook.jsp?bookid=<%= id %>','show',400,450)" >查看详细资料</a> </td>
</tr>
</form>
</table>

<br>
<p><a href="javascript:window.close()">关闭窗口</a></p>
</div>
</body>
</html>

81,110

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧