wood burning stoves 2.0*
The moose likes Servlets and the fly likes Sorry For Throwing Lengthy Codes At This Site, But I Really Need Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sorry For Throwing Lengthy Codes At This Site, But I Really Need Help" Watch "Sorry For Throwing Lengthy Codes At This Site, But I Really Need Help" New topic
Author

Sorry For Throwing Lengthy Codes At This Site, But I Really Need Help

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have been trying to work on a personal project which simulates on-line purchasing of books for days. This project does not use a database and this bookstore only has three books. The information about these three books are provided in a file called Catalog.jsp. After many attempts, I have decided to declare that my knowledge about JSP is too limited. I just cannot figure out the puzzle. And here I am to ask for help. (All my JSP pages are in the "bookstore" subdirectory, and will be listed following a short description of what I am doing. Then, I am going to show the error messages. The problems are that I do not know exactly what to do with several JSP pages involved.) I know that I am throwing something big at you, but I do not know what to do.
Description of what I am doing:
The first page (HomePage.jsp) is a very simple HTML like page which gives users three categories of books to select. In case that a user chooses to view the category of children's books, "CatalogPage.jsp" declares an array of item IDs, looks them up by means of a bean "Item.jsp" in the "Catalog.jsp" and put their corresponding Item entry into the items array. The Item contains a short description and a price, using the item ID as the unique key. Please bear with me. I am going to present my source codes:
HomePage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Home Page of the Bookstore</title></head>
<body BGCOLOR="#FDF5E6" ALINK="#52B552" VLINK="#299C39" LINK="#218429">
<CENTER><H1><FONT COLOR="#218429">Welcome to the On-Line Bookstore</FONT></H1></CENTER>
<CENTER><H3><FONT COLOR="#218429">Classification of Our Books</FONT></H3></CENTER>
<CENTER>
<TABLE>
<UL>
<TR><TD><LI TYPE=disc><STRONG><FONT SIZE="3"><A href="/bookstore/KidsBooksPage.jsp">Children</A></FONT></STRONG></LI></TD></TR>
<TR><TD><LI TYPE=disc><STRONG><FONT SIZE="3"><A href="/bookstore/TechBooksPage.jsp">Internet Technology</A></FONT></STRONG></LI></TD></TR>
<TR><TD><LI TYPE=disc><STRONG><FONT SIZE="3"><A href="/bookstore/ScFicBooksPage.jsp">Science Fiction</A></FONT></STRONG></LI></TD></TR>
</UL>
</TABLE>
</CENTER>
</body></html>
KidsBooksPage.jsp initializes the IDs of book items and CatalogPage.jsp looks those IDs up in the Catalog.jsp, and prints the info about the books in the browser window.
KidsBooksPage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>This Page Provides ID of Three Children's Books</TITLE>
</HEAD>
<BODY>
<%--
/** This class "extends" CatalogPage.jsp that
* displays a destination page listing three famous kids-book
* series. Orders will be sent to the OrderPage.jsp.
* <P>
*/
--%>
<%@ page extends="CatalogPage" %>
<%!
String[] ids = { "lewis001", "alexander001", "rowling001" };
setItems(ids);
setTitle("All-Time Best Children's Fantasy Books");
%>
</BODY>
</HTML>
CatalogPage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Catalog Page Provides a List of Books in Each Category</TITLE>
</HEAD>
<% page import="
java.io.*,
javax.servlet.*,
javax.servlet.http.*,
java.util.*,
bookstore.*"
%>
<%!
private Item[] items;
private String[] itemIDs;
private String title;
%>
<%--
/** Given an array of item IDs, look them up in the
* Catalog and put their corresponding Item entry
* into the items array. The Item contains a short
* description, and a price,
* using the item ID as the unique key.
* <P>
*/
--%>
<%
<jsp:useBean id="item"
class="bookstore.Item"
scope="session" />
<jsp:getProperty name="item" property="items" />
<jsp:getProperty name="item" property="title" />

protected void setItems(String[] itemIDs) {
this.itemIDs = itemIDs;
items = new Item[itemIDs.length];
for(int i=0; i<items.length; i++) {
items[i] = Catalog.getItem(itemIDs[i]);
}
}
protected void setTitle(String title) {
this.title = title;
}
%>
<%--
/** First display title, then, for each catalog item,
* put its short description in a level-two (H2) heading
* with the price in parentheses. Below each entry, put an
* order button that submits info to the OrderPage.jsp for
* the associated catalog entry.
* <P>
*/
--%>

<% response.setContentType("text/html"); %>
<% if (items == null) {
response.sendError(response.SC_NOT_FOUND,
"Missing Items.");
return;
}
%>
<BODY BGCOLOR="#FDF5E6" ALINK="#52B552" VLINK="#299C39" LINK="#218429">
<H1 ALIGN="CENTER"><%= title %></H1>
<%!
Item item;
%>
<%
for(int i=0; i<items.length; i++) {
%>
<HR>
<%
item = items[i];
// Show error message if subclass lists item ID
// that's not in the catalog.
if (item == null) {
%>
<FONT COLOR="RED">Unknown item ID <%= itemIDs[i] %></FONT>
<% } else {
%>
<P>
<%
String formURL =
"OrderPage";
// Pass URLs that reference own site through encodeURL.
formURL = response.encodeURL(formURL);
%>
<FORM ACTION="<%= formURL %>">
<INPUT TYPE="HIDDEN" NAME="itemID" VALUE="<%= item.getItemID() %>">
<CENTER><H3><%= item.getShortDescription() %></H3></CENTER>
<P><CENTER>
<B><FONT SIZE="3"><A href="<%= item.getItemID() %>">View The Description of the Book</A></FONT></B></CENTER>
<P><CENTER>
<INPUT TYPE="SUBMIT" VALUE="Add to Checkout Cart">
</CENTER><P>
</FORM>
<%
}
}
%>
<HR>
</BODY>
</HTML>
The CatalogPage.jsp looks IDs up by means of a bean "Item.jsp"
Item.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>The itemID uniquely identifies the item</TITLE>
</HEAD>
<BODY>
<%--
/** Describes a catalog item for on-line store. The itemID
* uniquely identifies the item, the short description
* gives brief info like the book title and author,
* the long description describes the item in a couple
* of sentences, and the cost gives the current per-item price.
* Both the short and long descriptions can contain HTML
* markup.
* <P>
*/
--%>
<%!
private String itemID;
private String shortDescription;
private String longDescription;
private double cost;
%>
<%--
A bean class must have a zero-argument constructor
--%>
<%
public Item() {}
public Item(String itemID, String shortDescription,
String longDescription, double cost) {
setItemID(itemID);
setShortDescription(shortDescription);
setLongDescription(longDescription);
setCost(cost);
}

public String getItemID() {
return(itemID);
}
protected void setItemID(String itemID) {
this.itemID = itemID;
}
public String getShortDescription() {
return(shortDescription);
}
protected void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getLongDescription() {
return(longDescription);
}
protected void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
public double getCost() {
return(cost);
}
protected void setCost(double cost) {
this.cost = cost;
}
%>
</BODY>
</HTML>
Catalog.jsp is where the information about the three children's books is provided:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>This file is a database that provides the information about all books in the library</TITLE>
</HEAD>
<BODY>
<%--
/** A catalog listing the items available in inventory.
* <P>
*/
--%>
<%!
private static Item[] items =
{ new Item("lewis001",
"<I>The Chronicles of Narnia</I> by C.S. Lewis",
"The classic children's adventure pitting " +
"Aslan the Great Lion and his followers\n" +
"against the White Witch and the forces " +
"of evil. Dragons, magicians, quests, \n" +
"and talking animals wound around a deep " +
"spiritual allegory. Series includes\n" +
"<I>The Magician's Nephew</I>,\n" +
"<I>The Lion, the Witch and the " +
"Wardrobe</I>,\n" +
"<I>The Horse and His Boy</I>,\n" +
"<I>Prince Caspian</I>,\n" +
"<I>The Voyage of the Dawn " +
"Treader</I>,\n" +
"<I>The Silver Chair</I>, and \n" +
"<I>The Last Battle</I>.",
19.95),
new Item("alexander001",
"<I>The Prydain Series</I> by Lloyd Alexander",
"Humble pig-keeper Taran joins mighty " +
"Lord Gwydion in his battle against\n" +
"Arawn the Lord of Annuvin. Joined by " +
"his loyal friends the beautiful princess\n" +
"Eilonwy, wannabe bard Fflewddur Fflam," +
"and furry half-man Gurgi, Taran discovers " +
"courage, nobility, and other values along\n" +
"the way. Series includes\n" +
"<I>The Book of Three</I>,\n" +
"<I>The Black Cauldron</I>,\n" +
"<I>The Castle of Llyr</I>,\n" +
"<I>Taran Wanderer</I>, and\n" +
"<I>The High King</I>.",
19.95),
new Item("rowling001",
"<I>The Harry Potter Trilogy</I> by " +
"J.K. Rowling",
"The first three of the popular stories " +
"about wizard-in-training Harry Potter\n" +
"topped both the adult and children's " +
"best-seller lists. Series includes\n" +
"<I>Harry Potter and the " +
"Sorcerer's Stone</I>,\n" +
"<I>Harry Potter and the " +
"Chamber of Secrets</I>, and\n" +
"<I>Harry Potter and the " +
"Prisoner of Azkaban</I>.",
25.95)
};
%>
<%
public static Item getItem(String itemID) {
%>
<%!
Item item;
%>
<%
if (itemID == null) {
return(null);
}
for(int i=0; i<items.length; i++) {
item = items[i];
if (itemID.equals(%><%= item.getItemID() %><%)) {
return(item);
}
}
return(null);
}
%>
</BODY>
</HTML>

Error Messages:
Compilation of 'C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java' failed:

C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:45: invalid method declaration; return type required
probably occurred due to an error in /bookstore/KidsBooksPage.jsp line 19:
setItems(ids);
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:45: expected
probably occurred due to an error in /bookstore/KidsBooksPage.jsp line 19:
setItems(ids);
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:45: ')' expected
probably occurred due to an error in /bookstore/KidsBooksPage.jsp line 19:
setItems(ids);
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:46: invalid method declaration; return type required
probably occurred due to an error in /bookstore/KidsBooksPage.jsp line 20:
setTitle("All-Time Best Children's Fantasy Books");
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:46: illegal start of type
probably occurred due to an error in /bookstore/KidsBooksPage.jsp line 20:
setTitle("All-Time Best Children's Fantasy Books");
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:46: expected
probably occurred due to an error in /bookstore/KidsBooksPage.jsp line 20:
setTitle("All-Time Best Children's Fantasy Books");
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:39: cannot resolve symbol
(No more information available, probably caused by another error)
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:45: cannot resolve symbol
probably occurred due to an error in /bookstore/KidsBooksPage.jsp line 19:
setItems(ids);
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:50: cannot resolve symbol
(No more information available, probably caused by another error)
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:63: cannot resolve symbol
(No more information available, probably caused by another error)
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:65: incompatible types
(No more information available, probably caused by another error)
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:68: getPageContext(javax.servlet.Servlet,javax.servlet.ServletRequest,javax.servlet.ServletResponse,java.lang.String,boolean,int,boolean) in javax.servlet.jsp.JspFactory cannot be applied to (jsp_servlet._bookstore._kidsbookspage,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,,boolean,int,boolean)
(No more information available, probably caused by another error)
C:\weblogic\myserver\classfiles\jsp_servlet\_bookstore\_kidsbookspage.java:86: cannot resolve symbol
(No more information available, probably caused by another error)

Wed Feb 28 23:00:15 EST 2001
Saran Vel
Ranch Hand

Joined: Nov 03, 2000
Posts: 111
Hi,
I tried to figureout your problem..
But have some doubts abt the following things..
--If u want to use the bean then why you declared item as a jsp..?
U can make item as a bean i think..
-- I'm not sure whether these declarations work for u'r item file which is actually a jsp..
<jsp:useBean id="item" class="item" scope="session" />
<jsp:getProperty name="item" property="items" />
<jsp:getProperty name="item" property="title" />
-- U can debug u'r jsp files from the java file (which is actually a servlet) which u can find by giving a search for example u can give a search for homepage.java (since i'm not sure which server u r using).
Sorry i cann't help u much..
Saran


Saran
Sun Certified Java2 Programmer
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Saran: Thank you. You are very modest. In fact, you have correctly pointed out the biggest mistake (among many other minor ones) that I made.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorry For Throwing Lengthy Codes At This Site, But I Really Need Help
 
Similar Threads
Storing of Data
opening database in servlet
What Is The Proper Way to Extend Another Class In JSP?
session tracking remove item (urgent)
Who's using Jersey for JAX-RS (JSR-311)?