aspose file tools*
The moose likes Servlets and the fly likes servlet exception error in my code(urgent) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet exception error in my code(urgent)" Watch "servlet exception error in my code(urgent)" New topic
Author

servlet exception error in my code(urgent)

rupa agarwal
Greenhorn

Joined: Nov 21, 2000
Posts: 24
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.
*/

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


[This message has been edited by maha anna (edited December 10, 2000).]
rupa agarwal
Greenhorn

Joined: Nov 21, 2000
Posts: 24

plz help me this is urgent
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

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Why in the name of all that is reasonable are you ignoring the data contained in the various exceptions that you catch and discard? You should be at least printing the exception to System.out. With JSP you can define an errorPage in the page directive and have it dump the ServletException in some usable form.
Bill

------------------
author of:
Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
Hi rupa,
Why are u not catching exception at line
while(rss.next())

Because you are trying to send 'null' in case if your 'execquery' method result in exception. Are you sure 'itemnum' in table is character? May because of this you are getting the exception,
regards,
R V Holla.
rupa agarwal
Greenhorn

Joined: Nov 21, 2000
Posts: 24
hi there i guess that is a problem with my itemnum coz it if of type integer it might be giving an error what shld i do to solve this bug plz suggest
rupa agarwal
Greenhorn

Joined: Nov 21, 2000
Posts: 24
hi there plz could anybody help me regarding my code that i have posted i have not received a solutionto my problem
thanx
rupa
rupa agarwal
Greenhorn

Joined: Nov 21, 2000
Posts: 24
ghi therre i have yet not received a solution to my problem could someone guide me plz.
rupa agarwal
Greenhorn

Joined: Nov 21, 2000
Posts: 24
hi there can anyone help me in this code of mine i get servlet excepiton code .
Mahesh Eshwarappa
Greenhorn

Joined: Jan 04, 2001
Posts: 28
Hi,
Create a simple JSP for the second page and print the session values u entered in the first page.
Get in touch with the o/p u get.
Mahesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet exception error in my code(urgent)