Originally posted by rupa agarwal:
here is the code for my cart.
/*this is the first jsp file here i am using a bean to connect to my database book which i have
made using msaccess.this bean ha connect method to connect to database,
a method to execute the query and a close method to close the connection.
here in jsp we are retireving data from database depending on which category of book user clicks
on.here after adding items to cart when we click on viewcart.jsp forward tag is used to take user to second jsp page
which should show the cart items.
*/
<%@ page import ="java.util.*, java.io.*,java.sql.*" %>
<jsp:useBean id="ConnectM" class="rupa.project.ConnectMeBean" scope="session"/>
<html>
<head>
<title>The books database</title>
<script language=JavaScript>
var i=0;
var itemname;
function submitcategory(a)
{
document.myform.myfield.value = a ;
document.myform.submit();
}
function hi(){
document.myform.submit();
}
function hi1(b){
document.myform.myfield1.value = b;
document.myform.submit();
}
</script>
</head>
<body>
<h1>books datadase</h1><br>
<%
String category = "";
if(request.getParameter("myfield")!=null)
category = request.getParameter("myfield");
%>
<form name=myform>
<input type=hidden name="myfield" value="<%=category%>">
<A href="javascript:submitcategory('webdesigning')">Category1</a><br>
<A href="javascript:submitcategory('Java')">Category2</a><br>
<A href="javascript:submitcategory('Cooking')">Category3</a><br>
<A href="javascript:submitcategory('Travel')">Category4</a><br>
<A href="javascript:submitcategory('History')">Category5</a><br>
<% ResultSet r = null;
if(request.getParameter("myfield")!=null)
{
ConnectM.connectdb();
String CategoryName = request.getParameter("myfield");
r = ConnectM.execquery("select * from book where bkcat =" + "'" + CategoryName + "'");
while(r.next()){
String itemnum = r.getString(2);
String bkcat= r.getString(3);
String bktitle = r.getString(4);
String bkprice = r.getString(5);
String bkqty = r.getString(6);
String bkauthor = r.getString(7);
String bkdetail = r.getString(8);
%>
<table>
<tr><td>
<b><%=itemnum%></b></td>
<td><%=bkcat%></td>
<td><%=bktitle%></td>
<td><%=bkprice%></td>
<td><%=bkqty%></td>
<td><%=bkauthor%></td>
<td><%=bkdetail%><br></td>
<td><input type=checkbox name="select" value=<%=itemnum%>>
</tr>
</table>
<%
}
}
%>
<% HttpSession s = request.getSession(true);
Integer itemcount = (Integer) s.getValue("itemcount");
if(itemcount == null)
{
itemcount = new Integer(0);
}
if(request.getParameterValues("select")!=null)
{
String [] itemselected = request.getParameterValues("select");
String itemname = "";
int a = itemcount.intValue();
for(int i=a ; i< itemselected.length+a ;i++)
{
out.println("You selected."+itemselected[i-a] );
itemname = itemselected[i-a];
itemcount = new Integer(itemcount.intValue() + 1);
s.putValue("Item" +i,itemname);
s.putValue("itemcount",itemcount);
}
}
%>
<% out.println("u have" +itemcount.intValue() +"items");
for(int i=0;i <itemcount.intValue();i++)
{
String item = (String) s.getValue("Item" +i);
%>
VALUE<%=item%><br>
<% }
ConnectM.closedb();
%>
<input type=Submit value="add to cart" onClick="javascript:hi()">
<input type=hidden name="myfield1" value="">
<input type=button value="view cart" name="viewcart" onClick="javascript:hi1('viewcart')">
<% if(request.getParameter("myfield1")!=null)
{ if(request.getParameter("myfield1").equals("viewcart"))
{
%>
say hi
<jsp:forward page="/rupa/project/cart1.jsp" />
<%
}
else{
%>
say bye
<%}
}
%>
</form>
</body>
</html>
jsp file 2 is here/*this is the cart jsp file which should the user the cart items
here again the same bean is used and depending on the users selected items
it retrieves the info from the database. for that itemnumber.
where itemnum is filed in the database.here in this file i get a servlet exception
rest the first jsp file is working fine
*/
<%@ page import ="java.util.*, java.io.*,java.sql.*" %>
<jsp:useBean id="ConnectM1" class="rupa.project.ConnectMeBean" scope="session"/>
<html>
<head>
<title>The cart</title>
</head>
<body>
<h1>this is ur carty</h1><br>
<%
out.println("iisorry empty session");
ResultSet rss = null;
HttpSession s = request.getSession(true);
if(s == null)
{ out.println("sorry empty session");
}
ConnectM1.connectdb();
Integer itemcount1 = (Integer) s.getValue("itemcount");
if(itemcount1 == null)
{
out.println("error getting the value of itemcount");
itemcount1=new Integer(0);
out.println(itemcount1);
}
for(int i=0;i <itemcount1.intValue();i++)
{
String item1 = (String) s.getValue("Item" +i);
int item2 = Integer.parseInt(item1);
%>
VALUE<%=item2%>
<%
rss = ConnectM1.execquery("select * from book where itemnum =" + "'" + item2 + "'");
while(rss.next()){
String bkcat1= rss.getString(3);
String bktitle1 = rss.getString(4);
String bkprice1 = rss.getString(5);
%>
<form>
<table>
<tr><td>
<%=bkcat1%></td>
<td><%=bktitle1%></td>
<td><%=bkprice1%></td>
</tr>
</table>
</form>
<%}
}
ConnectM1.closedb();%>
</body>
</html>%>
</body>
</html>
here is the code for teh bean
/*this is the bean for conecting to the database*/
package rupa.project;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.net.*;
public class ConnectMeBean implements java.io.Serializable{
Connection con=null;
Statement s = null;
ResultSet rs = null;
public boolean connectdb(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc dbc:mage", "", "");
return true;
}catch(Exception e){
return false;
}
}
public ResultSet execquery(String Sql){
try{
//Sql = "select * from book";
s = con.createStatement();
rs = s.executeQuery(Sql);
return rs;
}catch(Exception e){
return null;
}
}
public void closedb(){
if(rs !=null) {
try{
rs.close();}
catch(SQLException ignore){}
}
if(s !=null) {
try{
s.close();}
catch(SQLException ignore){}
}
if(con !=null) {
try{
con.close();}
catch(SQLException ignore){}
}
}
}
i am using tomcat server 3.1 for my project.
the problem i am getting is with second jspfile ,the error i get is servletexception
plz help me solve this bug
thanx rupa