Boa Noite Senhores…
estou iniciando na linguagem Java, porém, com dúvidas e erros…
estou lendo o livro antigo ( (Apress)—Beginning JSP?, JSF? and Tomcat Web Development From Novice to Professionall—(2007) )
e desenvolvendo a pequena livraria on line do inicio do livro, nao to conseguindo compilar meu servlet…
segue codigo:
The Ebookshop Home Page index.jsp
<%@page language=“java” contentType=“text/html”%>
<%@page session=“true” import=“java.util.*, ebookshop.Book”%>
Your online Bookshop
<% // Scriptlet 1: check whether the book list is ready
Vector booklist = (Vector)session.getValue("ebookshop.list");
if (booklist == null) {
response.sendRedirect("/ebookshop/eshop");
}
else {
%>
Book:
<% // Scriptlet 2: copy the book list to the selection control
for (int i = 0; i < booklist.size(); i++) {
out.println("" + (String)booklist.elementAt(i) + "");
}
%>
Quantity:
<% // Scriptlet 3: check whether the shopping cart is empty
Vector shoplist = (Vector)session.getValue("ebookshop.cart");
if (shoplist != null && shoplist.size() > 0) {
%>
| TITLE | PRICE | QUANTITY | |
| <%=aBook.getTitle()%> | $<%=aBook.getPrice()%> | <%=aBook.getQuantity()%> |
<% } // if (shoplist.. } // if (booklist..else.. %>
Checkout.jsp
<%@page language=“java” contentType=“text/html”%>
<%@page session=“true” import=“java.util.*, ebookshop.Book” %>
Your online Bookshop - Checkout
| TITLE | PRICE | QUANTITY |
| <%=anOrder.getTitle()%> | $<%=anOrder.getPrice()%> | <%=anOrder.getQuantity()%> |
| TOTALS | $<%=(String)request.getAttribute("dollars")%> | <%=(String)request.getAttribute("books")%> |
Book.java
package ebookshop;
public class Book {
String title;
float price;
int quantity;
public Book(String t, float p, int q) {
title = t;
price = p;
quantity = q;
}
public String getTitle() { return title; }
public void setTitle(String t) { title = t; }
public float getPrice() { return price; }
public void setPrice(float p) { price = p; }
public int getQuantity() { return quantity; }
public void setQuantity(int q) { quantity = q; }
}
ShoppingServlet.java
package ebookshop;
import java.util.<em>;
import <a href="http://java.io">java.io</a>.</em>;
import javax.servlet.<em>;
import javax.servlet.http.</em>;
import ebookshop.Book;
public class ShoppingServlet extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doPost(req, res);
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(true);
Vector shoplist =
(Vector)session.getAttribute(“ebookshop.cart”);
String do_this = req.getParameter(“do_this”);
if (do_this == null) {
Vector blist = new Vector();
blist.addElement(“Beginning JSP, JSF and Tomcat. Zambon/Sekler $39.99”);
blist.addElement(“Beginning JBoss Seam. Nusairat $39.99”);
blist.addElement(“Founders at Work. Livingston $25.99”);
blist.addElement(“Business Software. Sink $24.99”);
blist.addElement(“Foundations of Security. Daswani/Kern/Kesavan $39.99”);
session.setAttribute(“ebookshop.list”, blist);
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/");
rd.forward(req, res);
}
else {
if (do_this.equals(“checkout”)) {
float dollars = 0;
int books = 0;
for (int i = 0; i < shoplist.size(); i++) {
Book aBook = (Book)shoplist.elementAt(i);
float price = aBook.getPrice();
int qty = aBook.getQuantity();
dollars += price * qty;
books += qty;
}
req.setAttribute(“dollars”, new Float(dollars).toString());
req.setAttribute(“books”, new Integer(books).toString());
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/Checkout.jsp");
rd.forward(req, res);
} // if (…checkout…
else {
if (do_this.equals(“remove”)) {
String pos = req.getParameter(“position”);
shoplist.removeElementAt((new Integer(pos)).intValue());
}
else if (do_this.equals(“add”)) {
boolean found = false;
Book aBook = getBook(req);
if (shoplist == null) { // the shopping cart is empty
shoplist = new Vector();
shoplist.addElement(aBook);
}
else { // update the #copies if the book is already there
for (int i = 0; i < shoplist.size() && !found; i++) {
Book b = (Book)shoplist.elementAt(i);
if (b.getTitle().equals(aBook.getTitle())) {
b.setQuantity(b.getQuantity() + aBook.getQuantity());
shoplist.setElementAt(b, i);
found = true;
}
} // for (i…
if (!found) { // if it is a new book => Add it to the shoplist
shoplist.addElement(aBook);
}
} // if (shoplist == null) … else …
} // if (…add…
session.setAttribute(“ebookshop.cart”, shoplist);
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/");
rd.forward(req, res);
} // if (…checkout…else
} // if (do_this…
} // doPost
private Book getBook(HttpServletRequest req) {
String myBook = req.getParameter(“book”);
int n = myBook.indexOf(’$’);
String title = myBook.substring(0, n);
String price = myBook.substring(n+1);
String qty = req.getParameter(“qty”);
return new Book(title, Float.parseFloat(price), Integer.parseInt(qty));
} // getBook
}
compile_it.bat
@echo off
set aname=ebookshop
set /P fname=Please enter the java file name without extension:
set fil=%aname%%fname%
echo *** compile_it.bat: compile src%fil%.java
javac -verbose -deprecation -Xlint:unchecked -classpath classes src%fil%.java
if %errorlevel% GTR 1 goto _PAUSE
echo *** compile_it.bat: move the class to the package directory
move /y src%fil%.class classes%fil%.class
:_PAUSE
pause
porem, devo estar errando na configuracao das variaveis de ambiente, pois quando vou utilizar essa .bat
gera um erro do tipo :
src\ebookshop.class (Acesso Negado)
minha configuracao das variaveis:
CATALINA_HOME = C:\TomCat
JAVA_HOME = C:\Program Files\Java\jdk1.7.0_09
Path = C:\Webserver\PHP5;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;%JAVA_HOME%\bin;%CATALINA_HOME%\bin
CLASSPATH = .;C:\Program Files\Java\jdk1.7.0_09\jre\lib*.*;C:\Program Files\Java\jdk1.7.0_09\src.zip;
alguem pode dar uma orientacao…
desde ja agradeço


