servlet not passing hidden forum parameter
I have a servlet which get itesm from a database and prints them to a html page i then have a number of buttons with each item. When a user clicks the button i have in hidden form fields the item id, title, and the image name.
My problem is the next page picks up the item id but not the other two values instead there just given null values can somebody help me get the other two values passed?
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
publicclass Catalogextends HttpServlet{
String title;
publicstaticint MAX_COUNT = 100;
int curr_item = 0;
privatestatic CatalogItem addedItem[] =new CatalogItem[MAX_COUNT];
privatestatic CatalogItem items[] =new CatalogItem[MAX_COUNT];
CatalogItem item;
CatalogItem details;
publicstatic CatalogItem getItem(int recordingid){
CatalogItem item;
for(int i=0; i<items.length; i++){
item = items[i];
addedItem[i] = items[i];
if (recordingid==item.getrecordingid()){
return(item);
}
}
return(null);
}
publicvoid doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
curr_item = 0;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Database connection code starts here
int recordingidDB;
String directorDB;
String titleDB;
String categoryDB;
String imageDB;
int durationDB;
String ratingDB;
String yearDB;
float priceDB;
int StockDB;
Connection conn =null;
// loading jdbc driver for mysql (help in mysql.jar file in classpath)
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e){
System.out.println(e);
}
// connecting to database
try{
// connection string for demos database, username demos, password demo-pass
conn = DriverManager.getConnection
("jdbc:MY URL HERE");
// System.out.println("Connection to database successful.");
}
catch(SQLException se){
System.out.println(se);
}
// Create select statement and execute it
try{
String category = request.getParameter("category");
// Build up the SQL statement from our data requirements
String selectSQL ="select recording_id, director, title, category, image_name, duration, rating, year_released, price, stock_count "+
"from video_recordings " +
" where category = " +"'" + category +"'";
System.out.println(selectSQL);
Statement stmt = conn.createStatement();
ResultSet rs1 = stmt.executeQuery(selectSQL);
while(rs1.next() && curr_item >< MAX_COUNT){
recordingidDB = rs1.getInt("recording_id");
directorDB = rs1.getString("director");
titleDB = rs1.getString("title");
categoryDB = rs1.getString("category");
imageDB = rs1.getString("image_name");
durationDB = rs1.getInt("duration");
ratingDB = rs1.getString("rating");
yearDB = rs1.getString("year_released");
priceDB = rs1.getFloat("price");
StockDB = rs1.getInt("stock_count");
item =
new CatalogItem
(recordingidDB, directorDB, titleDB, categoryDB, imageDB, durationDB, ratingDB, yearDB, priceDB, StockDB);
addedItem[curr_item] = item;
items[curr_item++] = item;
}
int ITEM_COUNT = curr_item;
String pagetitle ="Catalog Items";
out.println(
"<BODY BGCOLOR=\"#a00e0e\">\n" +
"<center><img src=\"http://localhost:8080/examples/LOGO.jpg\" width=350 height=200/></center>\n" +
"<p align=\"left\"><a href=\"http://localhost:8080/examples/servlet/coreservlets.category\">Pick Another Category </a>
\n" +"<p align=\"left\"><a href=\"http://localhost:8080/examples/servlet/coreservlets.coreservlets\">Home</a>
\n" +"<p align=\"left\"><a href=\"http://localhost:8080/examples/servlet/coreservlets.rented\">View dvds you have rented</a>
\n" +"<p align=\"left\"><a href=\"http://localhost:8080/examples/servlet/coreservlets.wishlist\">Wishlist</a>
\n" +"<p align=\"right\"><font color=\"White\"><H5>" + title +"</H5></font color></P>\n" +
"<H1 ALIGN=\"CENTER\">" + pagetitle +"</H1>\n");
// loop to go over each item in the array of catalogItem
for(int i=0; i<addedItem.length; i++){
details = items[i];
if (details ==null){
out.println("SORRY THERE HAS BEEN AN ERROR ");
}else{
// need to check if logged on here otherwise still won't work
for(int j=0; j><MAX_COUNT; j++){
String formURL =
"http://localhost:8080/examples/servlet/coreservlets.addRental";// needs changing
// Pass URLs that reference own site through encodeURL.
formURL = response.encodeURL(formURL);
out.println("><FORM ACTION=\"" + formURL +"\">\n" +
"<INPUT TYPE=\"HIDDEN\" NAME=\"recordingid\" " +
"VALUE=\"" + addedItem[j].getrecordingid() +"\">\n" +
"<INPUT TYPE=\"HIDDEN\" NAME=\"filmtitle\" " +
"VALUE=\"" + addedItem[j].gettitle() +"\">\n" +
"<INPUT TYPE=\"HIDDEN\" NAME=\"dvdimage\" " +
"VALUE=\"" + addedItem[j].getimage() +"\">\n" +
"<P>\n<CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\" " +
"VALUE=\"Rent this film\">\n" +
"</CENTER>\n<P>\n</FORM>");
out.println(
"<IMG SRC=\"http://localhost:8080/examples/images/video/" + addedItem[j].getimage() +"\"> " +
addedItem[j].gettitle() +"\n" +
"Price: " + addedItem[j].getprice() +"\n" +
"Stock: " + addedItem[j].getstock() +"\n" );
String formURL2 =
"http://localhost:8080/examples/servlet/coreservlets.buyFilm";
// Pass URLs that reference own site through encodeURL.
formURL2 = response.encodeURL(formURL2);
out.print("<FORM ACTION=\"" + formURL2 +"\">\n" +
"<INPUT TYPE=\"HIDDEN\" NAME=\"recordingid\" " +
"VALUE=\"" + addedItem[j].getrecordingid() +"\">\n" +
"<P>\n<CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\" " +
"VALUE=\"Buy this film\">\n" +
"</CENTER>\n<P>\n</FORM>");
String formURL3 =// chnage these url names
"http://localhost:8080/examples/servlet/coreservlets.cookie";
// Pass URLs that reference own site through encodeURL.
formURL3 = response.encodeURL(formURL3);
out.print("<FORM ACTION=\"" + formURL3 +"\">\n" +
"<INPUT TYPE=\"HIDDEN\" NAME=\"recordingid\" " +
"VALUE=\"" + addedItem[j].getrecordingid() +"\">\n" +
"<P>\n<CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\" " +
"VALUE=\"Add to wish list\">\n" +
"</CENTER>\n<P>\n</FORM>");
}
out.println("</BODY></HTML>");
}
}
// Close the stament and database connection
//(must remember to always do this)
stmt.close();
conn.close();
}catch(SQLException se){
System.out.println(se);
}
}
}

