This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes jsp:compare select statement error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp:compare select statement error" Watch "jsp:compare select statement error" New topic
Author

jsp:compare select statement error

shaz akhtar
Greenhorn

Joined: Jan 01, 2005
Posts: 11
Hello everyone

I am doing my project on a shopping cart. i have a little problem! ive got 2 tables..products and tempcart. The products table contains the fields idnumber and all the rest of the product information (such as title, description). The tempcart table has 2 fields. sessionid and idnumber. Each time a user selects an item, the idnumber and the sessionid is inserted into the tempcart. It works so far!! however the problem is i want to find out what products has that particular user selected during that session not products selected by all users. At the moment when the user selects a product they want to purchase i get a blank page of the cart and does not show the product details. the code i am usinmg is as follows:

<%@ page import="java.sql.*, java.util.*, java.io.* " session="true" %>

<%
Class.forName("com.mysql.jdbc.Driver").newInstance();

java.sql.Connection con1 = null;

Statement stmt1 = null;
ResultSet rs1 = null;
String queryStr1 = null;
String usernamereg1 = request.getParameter("usernamereg");
String strsessionid = request.getParameter("sessionid");
try{
con1 = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/databasename?user=user&password=password");

stmt1 = con1.createStatement();
queryStr1 = "select * from products prod, tempcart cart where prod.idnumber=cart.idnumber AND cart.sessionid = '"+ strsessionid +"'";
rs1 = stmt1.executeQuery(queryStr1);
%>
<html>


User <font color = "red"><b><%=usernamereg1%></font></b> Logged In
<center>
<font size=5>Shopping Cart</font>
</center>
<center>

<br>
<form action="deletecart.jsp" method=post>
<br>
<table cellpadding=2 cellspacing=2 align="right" border=1 width="70%">
<tbody>
<tr>
<th>Select</th>

<th>Title</th>
<th>Description</th>
<th>Category</th>
<th>Price</th>
<th>Image</th>
<th>Remove</th>
</tr>
<%
while (rs1.next())
{
String idnumber = rs1.getString(1);
String title = rs1.getString(2);
String description = rs1.getString(3);
String category = rs1.getString(5);
String price = rs1.getString(6);
String image = rs1.getString(7);


%>

<tr><td valign = 'Top'><input type="radio" name="dvddownload" value="<%=idnumber%>"</br></td>
<td valign = 'Top'> <%=title%> </td>
<td valign = 'Top'> <%=description%> </td>
<td valign = 'Top'> <%=category%> </td>
<td valign = 'Top'> <%=price%> </td>
<td valign = 'Top'> <img src="<%=image%>" border=1 width=90 height=120"> </td>
<td valign = 'Top'><input type="submit" name="removetempcart" value="Remove" </td>

</tr>

<%
} // end while ()
}catch(SQLException s) {%> <%= s%> <% }
finally{
//clean up.
try{
if (rs1!=null) rs1.close();
if (stmt1!=null) stmt1.close();
if (con1!=null) con1.close();
}catch (SQLException s) {}

}
%>
</table>
<br>
<td align=center colspan=2>

</td>
</tr>


</center>

</form>
<form action="check2.html" method=post>
<input type="Submit" value="Checkout">
</form>
<form action="list.jsp" method=post>

<input type="submit" value="Continue Shopping">

<input type="hidden" name="sessionid" value="<%=session.getId()%>"

</form>

</html>


i have got the sessionid as a hidden field, convert it to a string and compare it in the select statement.but it still doesnt work. I would really appreciate if anyone can help me with this.

thanks shaz!!!
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Two questions:

1.
Could the problem be on any of your other pages ? In particular, it might help if you list your "list.jsp" (assuming that's the one that allows users to add items to their shopping cart).

2.
Is there a special reason why you chose to use a hidden field to deliver the sessionID to/from the browser ? To the best of my knowledge this approach is a bit un-usual : people usually prefer to just call:

and rely on the session ID to travel *automatically* between client and server, relying on either cookies or "url rewrites" (it does, however, mean that either you ask your users to enable cookies on their browser, or you use the "encodeURL" statement whenever you render a link).
shaz akhtar
Greenhorn

Joined: Jan 01, 2005
Posts: 11
thanks so much that worked
im passing most values that i need from one jsp page to another in hidden fields, is there are a better way of doing it?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

For starters, there's no need to pass the sessionId this way.
Use:


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Passing parameters between pages:
it might be useful to store shared data under your "session" (or under "application", if you'd like the datum to be viewed by *all users*, not just by the user who entered it).
A simple example : http://www.jsptut.com/Sessions.jsp

you might also want to have a look at the apache Struts framework (or the new "Java Server Faces" ).
It involves *considerably* more work (deploying jar files, creating java classes, editing xml files), and I would *not* recommend it for a very simple test program. But my company does use it for "real world" applications, where it proved priceless.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:compare select statement error
 
Similar Threads
two dropdown menus
inserting data to a table
cookies problem
jsp:checking if user logged in
I M NOT ABLE TO DEFINE VARIABLES IN JSP.