Olá amigos!
Estou tentando rodar um exemplo do livro Java Para A Web Com Servlets, Jsp E Ejb
Porém estou tendo muitos duvidas.
A primeira delas é onde possue o unico erro visivel
DB Bean
package com.brainysoftware.burnaby;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.sql.*;
public class DbBean {
public String dbUrl = "jdbc:mysql://localhost:3306/banco1";
public String dbUserName = "root";
public String dbPassword = "root";
public void setDbUrl(String url) {
dbUrl = url;
}
public void setDbUserName(String userName) {
dbUserName = userName;
}
public void setDbPassword(String password) {
dbPassword = password;
}
public Hashtable getCategories() {
Hashtable categories = new Hashtable();
try {
Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
Statement s = connection.createStatement();
String sql = "SELECT CategoryId, Category FROM Categories" +
" ";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
categories.put(rs.getString(1), rs.getString(2) );
}
rs.close();
s.close();
connection.close();
}
catch (SQLException e) {}
return categories;
}
public ArrayList getSearchResults(String keyword) {
ArrayList products = new ArrayList();
try {
Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
Statement s = connection.createStatement();
String sql = "SELECT ProductId, Name, Description, Price FROM Products" +
" WHERE Name LIKE '%" + keyword.trim() + "%'" +
" OR Description LIKE '%" + keyword.trim() + "%'";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
Product product = new Product();
product.id = rs.getInt(1);
product.name = rs.getString(2);
product.description = rs.getString(3);
product.price = rs.getDouble(4);
products.add(product);
}
rs.close();
s.close();
connection.close();
}
catch (SQLException e) {}
return products;
}
public ArrayList getProductsInCategory(String categoryId) {
ArrayList products = new ArrayList();
try {
Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
Statement s = connection.createStatement();
String sql = "SELECT ProductId, Name, Description, Price FROM Products" +
" WHERE CategoryId=" + categoryId;
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
Product product = new Product();
product.id = rs.getInt(1);
product.name = rs.getString(2);
product.description = rs.getString(3);
product.price = rs.getDouble(4);
products.add(product);
}
rs.close();
s.close();
connection.close();
}
catch (SQLException e) {}
return products;
}
public Product getProductDetails(int productId) {
Product product = null;
try {
Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
Statement s = connection.createStatement();
String sql = "SELECT ProductId, Name, Description, Price FROM Products" +
" WHERE ProductId=" + Integer.toString(productId);
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
product = new Product();
product.id = rs.getInt(1);
product.name = rs.getString(2);
product.description = rs.getString(3);
product.price = rs.getDouble(4);
}
rs.close();
s.close();
connection.close();
}
catch (SQLException e) {}
return product;
}
public boolean insertOrder(String contactName, String deliveryAddress,
String ccName, String ccNumber, String ccExpiryDate, Hashtable shoppingCart) {
boolean returnValue = false;
long orderId = System.currentTimeMillis();
Connection connection = null;
try {
connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
connection.setAutoCommit(false);
Statement s = connection.createStatement();
String sql = "INSERT INTO Orders" +
" (OrderId, ContactName, DeliveryAddress, CCName, CCNumber, CCExpiryDate)" +
" VALUES" +
" (" + orderId + ",'" + contactName + "','" + deliveryAddress + "'," +
"'" + ccName + "','" + ccNumber + "','" + ccExpiryDate + "')";
s.executeUpdate(sql);
// now insert items into OrderDetails table
Enumeration enum = shoppingCart.elements();
while (enum.hasMoreElements()) {
ShoppingItem item = (ShoppingItem) enum.nextElement();
sql = "INSERT INTO OrderDetails (OrderId, ProductId, Quantity, Price)" +
" VALUES (" + orderId + "," + item.productId + "," +
item.quantity + "," + item.price + ")";
s.executeUpdate(sql);
}
s.close();
connection.commit();
connection.close();
returnValue = true;
}
catch (SQLException e) {
try {
connection.rollback();
connection.close();
}
catch (SQLException se) {}
}
return returnValue;
}
}
No trecho onde possue o enumeration o objeto que está sendo criado é de um nome reservado enum.
outro trecho que tambem fiquei na dúvida é com relação aos links da pagina
Controller Servlet
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.brainysoftware.burnaby.DbBean;
public class ControllerServlet extends HttpServlet {
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
System.out.println("initializing controller servlet.");
ServletContext context = config.getServletContext();
context.setAttribute("base", config.getInitParameter("base"));
context.setAttribute("imageUrl", config.getInitParameter("imageUrl"));
// instantiating the DbBean
DbBean dbBean = new DbBean();
// initialize the DbBean's fields
dbBean.setDbUrl(config.getInitParameter("dbUrl"));
dbBean.setDbUserName(config.getInitParameter("dbUserName"));
dbBean.setDbPassword(config.getInitParameter("dbPassword"));
// put the bean in the servlet context
// the bean will be accessed from JSP pages
context.setAttribute("dbBean", dbBean);
try {
// loading the database JDBC driver
Class.forName(config.getInitParameter("jdbcDriver"));
}
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
super.init(config);
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String base = "/jsp/";
String url = base + "Default.jsp";
String action = request.getParameter("action");
if (action!=null) {
if (action.equals("search"))
url = base + "SearchResults.jsp";
else if (action.equals("browseCatalog"))
url = base + "BrowseCatalog.jsp";
else if (action.equals("productDetails"))
url = base + "ProductDetails.jsp";
else if (action.equals("productDetails"))
url = base + "ProductDetails.jsp";
else if (action.equals("addShoppingItem") ||
action.equals("updateShoppingItem") ||
action.equals("deleteShoppingItem") ||
action.equals("displayShoppingCart"))
url = base + "ShoppingCart.jsp";
else if (action.equals("checkOut"))
url = base + "CheckOut.jsp";
else if (action.equals("order"))
url = base + "Order.jsp";
}
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(url);
requestDispatcher.forward(request, response);
}
}
Quando Clico em uma em um link da na pagina ele me mostra um caminho esquisito Null
tipo : http://localhost:8080/loja/null?action=browsecatalog&categoryid=4
Desde já obrigado!