Hi All,
I have a servlet that query out some record and forward it to a jsp page to display. I got an error which I have no clue what it's complaint about. here is my code.
package Product;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class ProductAdminServlet extends HttpServlet {
private static final String driver = "org.gjt.mm.mysql.Driver";
private static final String user = "webuser";
private static final String password = "login";
private static final String dbURL = "jdbc:mysql:///sentry";
private static final String driver = "org.gjt.mm.mysql.Driver";
private static final String user = "webuser";
private static final String password = "login";
private static final String dbURL = "jdbc:mysql:///sentry";
private Connection ObjConnection = null;
private PreparedStatement getStmt = null;
private PreparedStatement getStmt = null;
private ServletContext context;
public void init(ServletConfig config) throws ServletException {
super.init(config);
context = config.getServletContext();
try {
Class.forName(driver);
ObjConnection = DriverManager.getConnection(dbURL, user, password);
}
catch (ClassNotFoundException e) {
System.err.println("Unable to load database driver");
throw new ServletException("Unable to load database driver");
}
catch (SQLException e) {
System.err.println("Unable to connect to database");
throw new ServletException("Unable to connect to database");
}
}
public void service(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
String jsp = "";
String cmd = req.getParameter("cmd");
String idString = req.getParameter("id");
int id;
try { id = Integer.parseInt(idString); }
catch(NumberFormatException e) { id=0; };
//Get Product By ID
if (cmd.equals("get")) {
ProductBean product = getProductByID(1);
req.setAttribute("product", product);
jsp = "/list.jsp";
}
//Get All Products
if (cmd.equals("getall")){
Vector list = getAllProduct();
req.setAttribute("list", list);
jsp = "/product_list.jsp";
}
RequestDispatcher dispatcher;
dispatcher = context.getRequestDispatcher(jsp);
dispatcher.forward(req, res);
}
//This method will return a single record base on the id
public ProductBean getProductByID(int id) {
try {
ResultSet results;
synchronized (getStmt) {
getStmt.clearParameters();
getStmt = ObjConnection.prepareStatement("SELECT * FROM Product WHERE Product_ID=?");
getStmt.setInt(1, 1);
results = getStmt.executeQuery();
}
ProductBean product = null;
if (results.next()) {
product = makeBean(results);
}
if (results != null)
results.close();
return product;
}
catch (SQLException se) { return null; }
}
public Vector getAllProduct() {
try {
Vector list = new Vector();
ResultSet results;
Statement st = ObjConnection.createStatement();
results = st.executeQuery("SELECT * FROM Product");
while (results.next())
list.add(makeBean(results));
return list;
}
catch (SQLException se) { return null; }
}
//This method will take a record and turn it into the bean
public ProductBean makeBean(ResultSet results)
throws SQLException {
ProductBean product = new ProductBean();
product.setProductCode(results.getString("Product_Code"));
product.setProductType(results.getString("Product_Type"));
product.setProductDescription(results.getString("Product_Description"));
product.setProductForwardBalance(results.getInt("Product_Balance"));
product.setProductUnitPrice(results.getDouble("Product_Unit_Price"));
product.setProductUnitMeasure(results.getString("Product_Unit_Measure"));
product.setProductSellByYard(results.getInt("Product_Sell_By_Yard"));
//Return the product result
return product;
}
//Release Resources
public void destroy() {
try {
if (ObjConnection != null)
ObjConnection.close();
}
catch (SQLException e) { }
}
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
context = config.getServletContext();
try {
Class.forName(driver);
ObjConnection = DriverManager.getConnection(dbURL, user, password);
}
catch (ClassNotFoundException e) {
System.err.println("Unable to load database driver");
throw new ServletException("Unable to load database driver");
}
catch (SQLException e) {
System.err.println("Unable to connect to database");
throw new ServletException("Unable to connect to database");
}
}
public void service(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
String jsp = "";
String cmd = req.getParameter("cmd");
String idString = req.getParameter("id");
int id;
try { id = Integer.parseInt(idString); }
catch(NumberFormatException e) { id=0; };
//Get Product By ID
if (cmd.equals("get")) {
ProductBean product = getProductByID(1);
req.setAttribute("product", product);
jsp = "/list.jsp";
}
//Get All Products
if (cmd.equals("getall")){
Vector list = getAllProduct();
req.setAttribute("list", list);
jsp = "/product_list.jsp";
}
RequestDispatcher dispatcher;
dispatcher = context.getRequestDispatcher(jsp);
dispatcher.forward(req, res);
}
//This method will return a single record base on the id
public ProductBean getProductByID(int id) {
try {
ResultSet results;
synchronized (getStmt) {
getStmt.clearParameters();
getStmt = ObjConnection.prepareStatement("SELECT * FROM Product WHERE Product_ID=?");
getStmt.setInt(1, 1);
results = getStmt.executeQuery();
}
ProductBean product = null;
if (results.next()) {
product = makeBean(results);
}
if (results != null)
results.close();
return product;
}
catch (SQLException se) { return null; }
}
public Vector getAllProduct() {
try {
Vector list = new Vector();
ResultSet results;
Statement st = ObjConnection.createStatement();
results = st.executeQuery("SELECT * FROM Product");
while (results.next())
list.add(makeBean(results));
return list;
}
catch (SQLException se) { return null; }
}
//This method will take a record and turn it into the bean
public ProductBean makeBean(ResultSet results)
throws SQLException {
ProductBean product = new ProductBean();
product.setProductCode(results.getString("Product_Code"));
product.setProductType(results.getString("Product_Type"));
product.setProductDescription(results.getString("Product_Description"));
product.setProductForwardBalance(results.getInt("Product_Balance"));
product.setProductUnitPrice(results.getDouble("Product_Unit_Price"));
product.setProductUnitMeasure(results.getString("Product_Unit_Measure"));
product.setProductSellByYard(results.getInt("Product_Sell_By_Yard"));
//Return the product result
return product;
}
//Release Resources
public void destroy() {
try {
if (ObjConnection != null)
ObjConnection.close();
}
catch (SQLException e) { }
}
}
JSP Code
<%@ page import="java.util.*" %>
<jsp:useBean id="product" class="Product.ProductBean" scope="request"/>
<html>
<body>
<table width="600" border="0">
<tr bgcolor="#006699">
<td colspan="6" class="big_label">PRODUCTS ADMINISTRATION</td>
</tr>
<tr bgcolor="#000000">
<td width="100" class="table_label"> </td>
<td width="100" class="table_label">Product Code</td>
<td width="100" class="table_label">Product Type</td>
<td width="100" class="table_label">Description</td>
<td width="100" class="table_label">Balance</td>
<td width="100" class="table_label">Unit Price</td>
</tr>
<%
Vector v = (Vector)request.getAttribute("list");
Iterator i= v.iterator();
while (i.hasNext()) {
product = (Product.ProductBean)i.next();
%>
<tr>
<td class="table_data"> </td>
<td class="table_data"><jsp:getProperty name="product" property="productCode" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productType" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productDescription" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productForwardBalance" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productUnitPrice" /></td>
</tr>
<% } %>
<tr bgcolor="#006699">
<td colspan="6"> </td>
</tr>
</table>
</body>
</html>
<html>
<body>
<table width="600" border="0">
<tr bgcolor="#006699">
<td colspan="6" class="big_label">PRODUCTS ADMINISTRATION</td>
</tr>
<tr bgcolor="#000000">
<td width="100" class="table_label"> </td>
<td width="100" class="table_label">Product Code</td>
<td width="100" class="table_label">Product Type</td>
<td width="100" class="table_label">Description</td>
<td width="100" class="table_label">Balance</td>
<td width="100" class="table_label">Unit Price</td>
</tr>
<%
Vector v = (Vector)request.getAttribute("list");
Iterator i= v.iterator();
while (i.hasNext()) {
product = (Product.ProductBean)i.next();
%>
<tr>
<td class="table_data"> </td>
<td class="table_data"><jsp:getProperty name="product" property="productCode" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productType" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productDescription" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productForwardBalance" /></td>
<td class="table_data"><jsp:getProperty name="product" property="productUnitPrice" /></td>
</tr>
<% } %>
<tr bgcolor="#006699">
<td colspan="6"> </td>
</tr>
</table>
</body>
</html>
type Exception report
message Internal Server Error
description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
at org.apache.jsp.product_0005flist$jsp._jspService(product_0005flist$jsp.java:94)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:683)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:431)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:355)
at Product.ProductAdminServlet.service(ProductAdminServlet.java:61)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)
at java.lang.Thread.run(Thread.java:536)
Can someone help me out please
Thanks,
Duc
Chat with friends online, try MSN Messenger: Click Here
=========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at: http://archives.java.sun.com/jsp-interest.html http://java.sun.com/products/jsp/faq.html http://www.esperanto.org.nz/jsp/jspfaq.jsp http://www.jguru.com/faq/index.jsp http://www.jspinsider.com
