Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A simple doubt on Shopping Cart ! Kindly help !

Status
Not open for further replies.

da21

Programmer
Dec 4, 2002
23
US
Hi !

After a simple call to the database using sql.jsp file in the Shopping Cart.jsp file, how do I display the message retrieved from the database on the ShoppingCart.jsp page itself instead of displaying it on a new window.


The ShoppingCart.jsp file code as under:

<tr>
<form action="/ShoppingCart/sql.jsp" method="post">
<td width="496" bordercolor="#FFFFFF"
align="center" bgcolor="#688888" height="21" width="118"><font class="listhead"><b><input type="text" name = "tex" value="Text"></font>

and sql.jsp file as under:

<%

if (rs.next()) {
out.println("<tr>\n<td>" + rs.getString("name") + "</td>");
out.println("<td align=\"center\">" + rs.getString("owner") + "</td>");
out.println("<td align=\"center\">" + rs.getString("species") + "</td>");
out.println("<td align=\"center\">" + rs.getString("sex") + "</td>");
out.println("<td align=\"center\">" + rs.getString("birth") + "</td>\n</tr>");
}


%>

</body>
</html>

When i execute the ShoppingCart.jsp file all the information from the database is picked up using the sql.jsp file and displayed on a fresh window, but how do i make it display it on ShoppingCart.jsp page itself.
Eagerly awaiting to hear.Seems like a simple doubt that i am unable to understand

Thanks
 
Submit the form to the same page.
i.e. <form action="ShoppingCart.jsp" method="post">

and move the code from the sql.jsp to ShoppingCart.jsp. Add a hidden form field to work as a flag for form submission.

Code:
<tr>
<form action="ShoppingCart.jsp" method="post">
<input type="hidden" name="submitted" value="1"/>
<input type="text" name = "tex" value="Text"/>
........
........
</form>
<% 
  // at where you want the sql resultSet to appear
  if (request.getParameter("submitted") != null) { 
    // ... do you sql stuff  here
    rs = statement.executeQuery(...);
    if (rs.next()) {
%>
<tr>
  <td><%= rs.getString("name") %></td>
  <td align="center"><%= rs.getString("owner") %></td>
  <td align="center"><%= rs.getString("species") %></td>
  <td align="center"><%= rs.getString("sex") %></td>
  <td align="center"><%= rs.getString("birth") %></td>
</tr>
<%
    }
  }
%>



%>

</body>
</html>
 
Thanks a lot ! i was aware of this option.
But I thought may there was another another way out without touching the sql.jsp file which is used to access the database.

thanks
 
If you want to keep sql code separated in sql.jsp. you can use jsp include:

<jsp:inlcude page="sql.jsp"/>
 
Theoretically, shouldn't all of the business logic, including sql queries, stay in the class files?

I am just wondering because I am new to JSP and I want to do code in the most efficient manner....

thanks
 
Practically, or a world known approach, would be to use Model View 2, separating business logic from presentation. One of the most famous framework for that would be Struts.
If presentation has lot of user interaction components (form), you may want to go one step further and use JavaServer Faces.

Of course depending on your requirement, if only one or two pages require simple business logic, using Strut may be overkill.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top