A little puzzle involving servlet parameter passing  
Author Message
Aris





PostPosted: 2004-11-3 18:35:00 Top

java-programmer, A little puzzle involving servlet parameter passing Hi all!

Anyone who wouldn't mind helping me out with a servlet interaction puzzle,
please read on!

I'm putting together a piece of a slightly larger database
project involving servlets and an oracle database. The database holds an
inventory of cars being sold and their attributes: Make, Model, Year,
etc...
I've composed a Listing Manager Page that lists the cars up for sale
by a particular dealer by querying the database. There are some "submit"-
type buttons next to each listing that would ideally allow a user to View,
Edit, or Delete a car from the list. In order to perform any of these
functions on a certain car on the list, I've constructed it so that an
attribute (in this case, inventoryno) would have to be passed to another
servlet which will perform the action. My issue lies in the passing of the
appropriate "inventoryno" parameter. A while loop is employed to retrieve
all the listed cars of the dealer. In each iteration of the loop an
inventoryno variable is assigned a value corresponding to the car being
retrieved from the database. However, naturally, at the end of the loop,
only the last car's inventoryno is stored because the previous one is
overwritten. Thus, when I click on, say, the View button for a car that was
NOT the last car retrieved, still only the inventoryno of the last car is
passed to the receiving servlet and the last car is displayed. Therein lies
the bug. How can I implement a solution that would pass only the
inventoryno of the car listed on the page whose View button was pressed?
I've posted my code below in case it helps and you'll notice that I
tried using an array of strings, to no avail.

Thanks in advance for any suggestions!



import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;

public class ListingManagerServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)

throws ServletException, IOException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

//Get the customerid attribute from LoginServlet.

HttpSession session = request.getSession(true);
String id = (String)session.getAttribute("customerid");

out.println("<html>");
out.println("<head><title>Listing Manager Page</title></head>");
out.println("<body>");
out.println("<div align=\"center\"><font face=\"Helvetica\"");
out.println(" size=\"3\" color=\"#db1260\"><h1>Listing Manager
Page");
out.println("</h1></font></div>");
out.println("Welcome! ");
out.println("<br><p><font color=\"#000080\" face=\"Arial\">The
following are your listed items:</font></p><br><br>");
out.println("</body>");
out.println("</html>");


//Load JDBC Driver and connect to the database.

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@server9.engr.scu.edu:1521:dc81";
String dbuser = "coen2802";
String dbpswd = "coen2802";
Connection con = DriverManager.getConnection(url, dbuser, dbpswd);

String query = "select Condition, Make, Model, Year, Category,
Mileage, MSRP, InventoryNo, InvoicePrice, ListingDate from CarsInfo where
CustomerID = '"+id+"'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next())
{

int i = 0;
String[] inventoryno;
inventoryno = new String[20];

String condition = rs.getString("Condition");

String make = rs.getString("Make");
String model = rs.getString("Model");
String year = rs.getString("Year");
String category = rs.getString("Category");
String mileage = rs.getString("Mileage");
String msrp = rs.getString("MSRP");
String invoiceprice = rs.getString("InvoicePrice");
String listingdate = rs.getString("ListingDate");
inventoryno[i] = rs.getString("InventoryNo");


session.setAttribute("inventoryno", inventoryno[i]);

out.println("<table border='1' cellpadding='2' cellspacing='0' ");
out.println("width='600' font face=\"Arial\" size=\"2\">");
out.println("<tr bgcolor=\"#EBF0F3\" height=\"302\" ");
out.println("font-weight: bold>");

out.println("<td height=\"16\"><b>Condition</b></td>");
out.println("<td height=\"16\"><b>Make</b></td>");
out.println("<td height=\"16\"><b>Model</b></td>");
out.println("<td height=\"16\"><b>Year</b></td>");
out.println("<td height=\"16\"><b>Category</b></td>");
out.println("<td height=\"16\"><b>Mileage</b></td>");
out.println("<td height=\"16\"><b>MSRP</b></td>");
out.println("<td height=\"16\"><b>InvoicePrice</b></td>");
out.println("<td height=\"16\"><b>ListingDate</b></td>");
out.println("</tr>");

out.println("<tr height=\"302\">");
out.println("<td height=\"16\">" +condition+ "</td>");
out.println("<td height=\"16\">" +make+ "</td>");
out.println("<td height=\"16\">" +model+ "</td>");
out.println("<td height=\"16\">" +year+ "</td>");
out.println("<td height=\"16\">" +category+ "</td>");
out.println("<td height=\"16\">" +mileage+ "</td>");
out.println("<td height=\"16\">" +msrp+ "</td>");
out.println("<td height=\"16\">" +invoiceprice+ "</td>");
out.println("<td height=\"16\">" +listingdate+ "</td>");

out.println("</tr>");
out.println("</table>");



out.println("<table width='20%'>");
out.println("<td>");
out.println("<form
action=\"/carsonline/servlet/ViewServlet\">");
out.println("<input type=\"submit\" value=\"View\"
name=\"V1\">");
out.println("</form>");

out.println("</td>");
out.println("<td>");
out.println("<form
action=\"/carsonline/servlet/EditServlet\">");
out.println("<input type=\"submit\" value=\"Edit\"
name=\"E1\">");

out.println("</form>");
out.println("</td>");


out.println("<td>");
out.println("<form
action=\"/carsonline/servlet/DeleteServlet\">");
out.println("<input type=\"submit\" value=\"Delete\"
name=\"D1\">");


out.println("</form>");
out.println("</td>");
out.println("</tr>");


out.println("</table>");

out.println("<br><br><br><br>");


i++;


}


out.println("<form action=\"/carsonline/jsp/ListingCreation.jsp\">");
out.println("<input type=\"submit\" value=\"Create New Listing\" ");
out.println("name=\"CN1\">");
out.println("</form>");


out.close();
rs.close();
stmt.close();
con.close();

}

catch(Exception e)
{
out.println(e);

}

}


}





Here is the receiving servlet's code:


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;



public class ViewServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)

throws ServletException, IOException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

//Get the inventoryno attribute from ListingManagerServlet.

HttpSession session = request.getSession(true);
String inventoryno = (String)session.getAttribute("inventoryno");

//Load JDBC Driver and connect to the database.

try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@server9.engr.scu.edu:1521:dc81";
String dbuser = "coen2802";
String dbpswd = "coen2802";
Connection con = DriverManager.getConnection(url, dbuser,dbpswd);

String query = "select Condition, Make, Model, Year, Category,
Mileage, MSRP, InvoicePrice, Engine, Color, VIN#, Description, ListingDate
from CarsInfo where InventoryNo = '" +inventoryno+ "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{

String condition = rs.getString("Condition");
String make = rs.getString("Make");
String model = rs.getString("Model");
String year = rs.getString("Year");
String category = rs.getString("Category");
String mileage = rs.getString("Mileage");
String engine = rs.getString("Engine");
String color = rs.getString("Color");
String msrp = rs.getString("MSRP");
String invoiceprice = rs.getString("InvoicePrice");
String listingdate = rs.getString("ListingDate");
String description = rs.getString("Description");

out.println("<table border='1' cellpadding='2' cellspacing='0' ");
out.println("width='600' font fact=\"Arial\" size=\"2\">");
out.println("<tr bgcolor=\"#EBF0F3\" height =\"302\" ");
out.println("font-weight: bold>");

out.println("<td height=\"16\"><b>Condition</b></td>");
out.println("<td height=\"16\"><b>Make</b></td>");
out.println("<td height=\"16\"><b>Model </b></td>");
out.println("<td height=\"16\"><b>Year</b></td>");
out.println("<td height=\"16\"><b>Category</b></td>");
out.println("<td height=\"16\"><b>Engine </b></td>");
out.println("<td height=\"16\"><b>Color</b></td>");
out.println("<td height=\"16\"><b>Mileage</b></td>");
out.println("<td height=\"16\"><b>MSRP</b></td>");
out.println("<td height=\"16\"><b>InvoicePrice</b></td>");
out.println("<td height=\"16\"><b>ListingDate</b></td>");
out.println("</tr>");

out.println("<tr height=\"302\">");
out.println("<td height=\"16\">" +condition+ "</td>");
out.println("<td height=\"16\">" +make+ "</td>");
out.println("<td height=\"16\">" +model+ "</td>");
out.println("<td height=\"16\">" +year+ "</td>");
out.println("<td height=\"16\">" +category+ "</td>");
out.println("<td height=\"16\">" +engine+ "</td>");
out.println("<td height=\"16\">" +color+ "</td>");
out.println("<td height=\"16\">" +mileage+ "</td>");
out.println("<td height=\"16\">" +msrp+ "</td>");
out.println("<td height=\"16\">" +invoiceprice+ "</td>");
out.println("<td height=\"16\">" +listingdate+ "</td>");

out.println("</tr>");
out.println("</table>");

out.println("<br><br><br><br>");
out.println(description);


}

out.println("<form
action=\"/carsonline/servlet/ListingManagerServlet\">");
out.println("<input type=\"submit\" value=\"Return to Listing Manager
Page\" ");
out.println("name=\"LMP1\">");
out.println("</form>");



rs.close();
stmt.close();
con.close();

out.close();
}


catch(Exception e)
{
out.println(e);
}

}

}






 
cdver03





PostPosted: 2004-11-3 23:29:00 Top

java-programmer >> A little puzzle involving servlet parameter passing Your code demonstrates a lack of understanding of how html and servlets
interact. Before getting too far into it, I'd recommend taking one of the
excellent online tutorials available on Servlets and html. Here's one:
http://www-106.ibm.com/developerworks/java/edu/j-dw-javaservlets-i.html

Another thing to consider also, is that servlets are lousy at producing
HTML. JSPs are much better for that. As a general rule, use servlets to
process information and set things up, and use JSPs to produce the actual
HTML.

Having said that, here is a possible solution for this problem:

Here is pseudo code for what you want the html to look like:

<head... headings, etc.
<body>
for each car in the inventory incrementing i{
<form name=form<i> action=whatever>
<input type="hidden" name="inventoryNum" value="<inventoryNo[i]>" >
information about the car goes here
now put your submit buttons:
<input type="submit" name="delete">
<input type="submit name="edit"> etc.

</form>
} end for each car
</body>
</html>

This produces an HTML document with multiple forms. When you press the
submit button that is enclosed in a given form tag, it submits only the
inventory number enclosed in that form tag as a parameter. This parameter
can then be retrieved by the next servlet with the following code:
String inventoryNo = request.getParameter("inventoryNum");
This assumes "request" is the name you gave to the HttpServletRequest
parameter that is passed in to your doPost or doGet method.

The principle is that all <input> tags pertaining to a form must be enclosed
in the <form> tag. When a submit button for that form is pressed, thee
information in the <input> tags is passed in the form of parameters to the
next servlet.

Merrill

"Aris" <email***@***.com> wrote in message
news:ov2id.3809974$email***@***.com...
> Hi all!
>
> Anyone who wouldn't mind helping me out with a servlet interaction puzzle,
> please read on!
>
> I'm putting together a piece of a slightly larger database
> project involving servlets and an oracle database. The database holds an
> inventory of cars being sold and their attributes: Make, Model, Year,
> etc...
> I've composed a Listing Manager Page that lists the cars up for sale
> by a particular dealer by querying the database. There are some "submit"-
> type buttons next to each listing that would ideally allow a user to View,
> Edit, or Delete a car from the list. In order to perform any of these
> functions on a certain car on the list, I've constructed it so that an
> attribute (in this case, inventoryno) would have to be passed to another
> servlet which will perform the action. My issue lies in the passing of the
> appropriate "inventoryno" parameter. A while loop is employed to retrieve
> all the listed cars of the dealer. In each iteration of the loop an
> inventoryno variable is assigned a value corresponding to the car being
> retrieved from the database. However, naturally, at the end of the loop,
> only the last car's inventoryno is stored because the previous one is
> overwritten. Thus, when I click on, say, the View button for a car that
was
> NOT the last car retrieved, still only the inventoryno of the last car is
> passed to the receiving servlet and the last car is displayed. Therein
lies
> the bug. How can I implement a solution that would pass only the
> inventoryno of the car listed on the page whose View button was pressed?
> I've posted my code below in case it helps and you'll notice that I
> tried using an array of strings, to no avail.
>
> Thanks in advance for any suggestions!
>
>
>
> import java.io.*;
>
> import javax.servlet.http.*;
> import javax.servlet.*;
> import java.sql.*;
>
> public class ListingManagerServlet extends HttpServlet {
> public void doGet (HttpServletRequest request,
> HttpServletResponse response)
>
> throws ServletException, IOException
> {
>
> response.setContentType("text/html");
> PrintWriter out = response.getWriter();
>
> //Get the customerid attribute from LoginServlet.
>
> HttpSession session = request.getSession(true);
> String id = (String)session.getAttribute("customerid");
>
> out.println("<html>");
> out.println("<head><title>Listing Manager Page</title></head>");
> out.println("<body>");
> out.println("<div align=\"center\"><font face=\"Helvetica\"");
> out.println(" size=\"3\" color=\"#db1260\"><h1>Listing Manager
> Page");
> out.println("</h1></font></div>");
> out.println("Welcome! ");
> out.println("<br><p><font color=\"#000080\" face=\"Arial\">The
> following are your listed items:</font></p><br><br>");
> out.println("</body>");
> out.println("</html>");
>
>
> //Load JDBC Driver and connect to the database.
>
> try{
> Class.forName("oracle.jdbc.driver.OracleDriver");
> String url = "jdbc:oracle:thin:@server9.engr.scu.edu:1521:dc81";
> String dbuser = "coen2802";
> String dbpswd = "coen2802";
> Connection con = DriverManager.getConnection(url, dbuser, dbpswd);
>
> String query = "select Condition, Make, Model, Year, Category,
> Mileage, MSRP, InventoryNo, InvoicePrice, ListingDate from CarsInfo where
> CustomerID = '"+id+"'";
> Statement stmt = con.createStatement();
> ResultSet rs = stmt.executeQuery(query);
>
> while (rs.next())
> {
>
> int i = 0;
> String[] inventoryno;
> inventoryno = new String[20];
>
> String condition = rs.getString("Condition");
>
> String make = rs.getString("Make");
> String model = rs.getString("Model");
> String year = rs.getString("Year");
> String category = rs.getString("Category");
> String mileage = rs.getString("Mileage");
> String msrp = rs.getString("MSRP");
> String invoiceprice = rs.getString("InvoicePrice");
> String listingdate = rs.getString("ListingDate");
> inventoryno[i] = rs.getString("InventoryNo");
>
>
> session.setAttribute("inventoryno", inventoryno[i]);
>
> out.println("<table border='1' cellpadding='2' cellspacing='0' ");
> out.println("width='600' font face=\"Arial\" size=\"2\">");
> out.println("<tr bgcolor=\"#EBF0F3\" height=\"302\" ");
> out.println("font-weight: bold>");
>
> out.println("<td height=\"16\"><b>Condition</b></td>");
> out.println("<td height=\"16\"><b>Make</b></td>");
> out.println("<td height=\"16\"><b>Model</b></td>");
> out.println("<td height=\"16\"><b>Year</b></td>");
> out.println("<td height=\"16\"><b>Category</b></td>");
> out.println("<td height=\"16\"><b>Mileage</b></td>");
> out.println("<td height=\"16\"><b>MSRP</b></td>");
> out.println("<td height=\"16\"><b>InvoicePrice</b></td>");
> out.println("<td height=\"16\"><b>ListingDate</b></td>");
> out.println("</tr>");
>
> out.println("<tr height=\"302\">");
> out.println("<td height=\"16\">" +condition+ "</td>");
> out.println("<td height=\"16\">" +make+ "</td>");
> out.println("<td height=\"16\">" +model+ "</td>");
> out.println("<td height=\"16\">" +year+ "</td>");
> out.println("<td height=\"16\">" +category+ "</td>");
> out.println("<td height=\"16\">" +mileage+ "</td>");
> out.println("<td height=\"16\">" +msrp+ "</td>");
> out.println("<td height=\"16\">" +invoiceprice+ "</td>");
> out.println("<td height=\"16\">" +listingdate+ "</td>");
>
> out.println("</tr>");
> out.println("</table>");
>
>
>
> out.println("<table width='20%'>");
> out.println("<td>");
> out.println("<form
> action=\"/carsonline/servlet/ViewServlet\">");
> out.println("<input type=\"submit\" value=\"View\"
> name=\"V1\">");
> out.println("</form>");
>
> out.println("</td>");
> out.println("<td>");
> out.println("<form
> action=\"/carsonline/servlet/EditServlet\">");
> out.println("<input type=\"submit\" value=\"Edit\"
> name=\"E1\">");
>
> out.println("</form>");
> out.println("</td>");
>
>
> out.println("<td>");
> out.println("<form
> action=\"/carsonline/servlet/DeleteServlet\">");
> out.println("<input type=\"submit\" value=\"Delete\"
> name=\"D1\">");
>
>
> out.println("</form>");
> out.println("</td>");
> out.println("</tr>");
>
>
> out.println("</table>");
>
> out.println("<br><br><br><br>");
>
>
> i++;
>
>
> }
>
>
> out.println("<form action=\"/carsonline/jsp/ListingCreation.jsp\">");
> out.println("<input type=\"submit\" value=\"Create New Listing\" ");
> out.println("name=\"CN1\">");
> out.println("</form>");
>
>
> out.close();
> rs.close();
> stmt.close();
> con.close();
>
> }
>
> catch(Exception e)
> {
> out.println(e);
>
> }
>
> }
>
>
> }
>
>
>
>
>
> Here is the receiving servlet's code:
>
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.sql.*;
>
>
>
> public class ViewServlet extends HttpServlet {
> public void doGet (HttpServletRequest request,
> HttpServletResponse response)
>
> throws ServletException, IOException
> {
>
> response.setContentType("text/html");
> PrintWriter out = response.getWriter();
>
> //Get the inventoryno attribute from ListingManagerServlet.
>
> HttpSession session = request.getSession(true);
> String inventoryno = (String)session.getAttribute("inventoryno");
>
> //Load JDBC Driver and connect to the database.
>
> try{
> Class.forName("oracle.jdbc.driver.OracleDriver");
> String url = "jdbc:oracle:thin:@server9.engr.scu.edu:1521:dc81";
> String dbuser = "coen2802";
> String dbpswd = "coen2802";
> Connection con = DriverManager.getConnection(url, dbuser,dbpswd);
>
> String query = "select Condition, Make, Model, Year, Category,
> Mileage, MSRP, InvoicePrice, Engine, Color, VIN#, Description, ListingDate
> from CarsInfo where InventoryNo = '" +inventoryno+ "'";
> Statement stmt = con.createStatement();
> ResultSet rs = stmt.executeQuery(query);
> while (rs.next())
> {
>
> String condition = rs.getString("Condition");
> String make = rs.getString("Make");
> String model = rs.getString("Model");
> String year = rs.getString("Year");
> String category = rs.getString("Category");
> String mileage = rs.getString("Mileage");
> String engine = rs.getString("Engine");
> String color = rs.getString("Color");
> String msrp = rs.getString("MSRP");
> String invoiceprice = rs.getString("InvoicePrice");
> String listingdate = rs.getString("ListingDate");
> String description = rs.getString("Description");
>
> out.println("<table border='1' cellpadding='2' cellspacing='0' ");
> out.println("width='600' font fact=\"Arial\" size=\"2\">");
> out.println("<tr bgcolor=\"#EBF0F3\" height =\"302\" ");
> out.println("font-weight: bold>");
>
> out.println("<td height=\"16\"><b>Condition</b></td>");
> out.println("<td height=\"16\"><b>Make</b></td>");
> out.println("<td height=\"16\"><b>Model </b></td>");
> out.println("<td height=\"16\"><b>Year</b></td>");
> out.println("<td height=\"16\"><b>Category</b></td>");
> out.println("<td height=\"16\"><b>Engine </b></td>");
> out.println("<td height=\"16\"><b>Color</b></td>");
> out.println("<td height=\"16\"><b>Mileage</b></td>");
> out.println("<td height=\"16\"><b>MSRP</b></td>");
> out.println("<td height=\"16\"><b>InvoicePrice</b></td>");
> out.println("<td height=\"16\"><b>ListingDate</b></td>");
> out.println("</tr>");
>
> out.println("<tr height=\"302\">");
> out.println("<td height=\"16\">" +condition+ "</td>");
> out.println("<td height=\"16\">" +make+ "</td>");
> out.println("<td height=\"16\">" +model+ "</td>");
> out.println("<td height=\"16\">" +year+ "</td>");
> out.println("<td height=\"16\">" +category+ "</td>");
> out.println("<td height=\"16\">" +engine+ "</td>");
> out.println("<td height=\"16\">" +color+ "</td>");
> out.println("<td height=\"16\">" +mileage+ "</td>");
> out.println("<td height=\"16\">" +msrp+ "</td>");
> out.println("<td height=\"16\">" +invoiceprice+ "</td>");
> out.println("<td height=\"16\">" +listingdate+ "</td>");
>
> out.println("</tr>");
> out.println("</table>");
>
> out.println("<br><br><br><br>");
> out.println(description);
>
>
> }
>
> out.println("<form
> action=\"/carsonline/servlet/ListingManagerServlet\">");
> out.println("<input type=\"submit\" value=\"Return to Listing Manager
> Page\" ");
> out.println("name=\"LMP1\">");
> out.println("</form>");
>
>
>
> rs.close();
> stmt.close();
> con.close();
>
> out.close();
> }
>
>
> catch(Exception e)
> {
> out.println(e);
> }
>
> }
>
> }
>
>
>
>
>
>


 
Oscar kind





PostPosted: 2004-11-4 2:08:00 Top

java-programmer >> A little puzzle involving servlet parameter passing Aris <email***@***.com> wrote:
> I'm putting together a piece of a slightly larger database
> project involving servlets and an oracle database. The database holds an
> inventory of cars being sold and their attributes: Make, Model, Year,
> etc...

<cut: description of page with car list with view/edit/delete buttons>


The first thing to realise is that instead of a hidden form parameter
(that changes with each car that's added to the page), you probably want
something that's associated with the buttons for the car. This you can
generate as the value of the button, as in this snippet of HTML:

<button name="view" value="34" onclick="this.value=34">View</button>

The onclick attribute is needed because Microsoft deliberately implements
the button tag differently from the standard. Also, IE always submits the
HTML(!) between the open and close tags as value, instead of the value
attribute (so you also need to filter out all non-numbers).


The second part is that you can get the request parameters in the servlet
that displays the car using HttpServletRequest#getParameter(String).


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2