Hi All!
I've been working on a simple MVC style / struts web application which will allow a user to view a product database and have the option to update a record. The initial index.jsp page works and forwards to the right locations, the update page displays but is producing another error but that is not within the scope of this question.
When i click to view the product database, I get the following error in Apache Tomcat (note I am using built in Tomcat within Netbeans 5.0)
I have done searches on the Web and they say that this method is being called recursively? However, when I run a System.out.println to show the SQL query it displays fine with no problems. Also it is not showing errors with other methods, so I assume that it is stuck on this getPid method.
I hope someone can shed some light on this matter.
Thanks
Apache Tomcat Error
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781) org.apache.jsp.viewprod_jsp._jspService(viewprod_jsp.java:112) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362) org.apache.struts.action.ActionServlet.processForward(ActionServlet.java:1848) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1569) org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:492) javax.servlet.http.HttpServlet.service(HttpServlet.java:689) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
java.lang.StackOverflowError elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid
.... THIS CONTINUES TILL THE END OF THE PAGE ....
(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid(UpdateBean.java:16) elecstore.UpdateBean.getPid
(UpdateBean.java:16)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
Apache Tomcat/5.5.9
UpdateBean.java
package elecstore;
import java.sql.*;
import java.sql.Date;
import java.text.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
import elecstore.*;
public class UpdateBean {
Products p = null;
public int getPid() {
return getPid();
}
public void setPid(int pid) {
setPid(pid);
}
public String getProdId() {
return getProdId();
}
public void setProdId (String prodId) {
setProdId(prodId);
}
public String getProdType() {
return getProdType();
}
public void setProdType (String prodType) {
setProdType(prodType);
}
public String getMan() {
return getMan();
}
public void setMan (String man) {
setMan(man);
}
public String getProduct() {
return getProduct();
}
public void setProduct (String product) {
setProduct(product);
}
public int getQty() {
return getQty();
}
public void setQty (int qty) {
setQty(qty);
}
public String getPrice() {
return getPrice();
}
public void setPrice (String price) {
setPrice(price);
}
public Date getDateAdded() {
return getDateAdded();
}
public void setDateAdded (Date dateAdded) {
setDateAdded(dateAdded);
}
public boolean getStockOnOrder() {
return getStockOnOrder();
}
public void setStockOnOrder (boolean stockOnOrder) {
setStockOnOrder(stockOnOrder);
}
public UpdateBean() throws SQLException, NamingException
{
String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc:odbc:qc310";
String user = "Lukas";
String password = "12345";
System.out.println("TEST HERE - UpdateBean");
Connection con = null;
Connection con2 = null;
Statement select = null;
PreparedStatement updateProduct = null;
ResultSet rs = null;;
ResultSet rs2 = null;
try {
System.out.println("TEST HERE - making database connection...");
// Load in the JDBC-ODBC driver
Class.forName(jdbcDriver);
// Create connection
con = DriverManager.getConnection(dbURL, user, password);
System.out.println("\n...Connection to Electronic Store Database Established\n");
System.out.println("TEST HERE - database connection ok...");
Statement stmt = con.createStatement();
System.out.println("SQL Statement created...");
rs = stmt.executeQuery("SELECT * FROM Elecstore");
while (rs.next()) {
p = new Products(rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getInt(6),
rs.getString(7),
rs.getDate(8),
rs.getBoolean(9));
System.out.println(p.getPid() + " " + p.getProdId() + "\t" + p.getProdType() + "\t\t" + p.getMan() + "\t\t" + p.getProduct() + "\t\t\t" + p.getPrice() + "\t\t\t" + p.getQty() + "\t\t\t" + p.getDateAdded() + "\t\t\t" + p.getStockOnOrder());
}
rs.close();
}
catch(Exception e) {
e.printStackTrace();;
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con2 = DriverManager.getConnection("jdbc:odbc:qc310");
updateProduct = con.prepareStatement("UPDATE Elecstore SET Qty = ?, Price = ? WHERE ProductID = ?");
updateProduct.setInt (1, p.getQty());
updateProduct.setString (2, p.getPrice());
updateProduct.setString (3, p.getProdId());
updateProduct.executeUpdate();
if (!rs2.next())
throw new SQLException("Missing Products data row");
}
catch(Exception e) {
System.out.println(e);
}
finally {
try { rs.close(); } catch (Exception ex) {}
try { con.close(); } catch (Exception ex) {}
}
}
}
Products.java
package elecstore;
import java.sql.Date;
/**
* This domain object represents a player in a sports league.
* The data attributes are all package-private to allow access to them
* in the {@link RegisterService} class.
*/
public class Products {
int pid;
int qty;
String prodId;
String prodType;
String man;
String product;
String price;
Date dateAdded;
boolean stockOnOrder;
/**
* This is the constructor. It is package-private to prevent
* misuse. The {@link RegisterServer} method should be used to
* create a new player object.
*/
//Product(String prodID) {
// this(+1, "", prodID, "", "", "", "");
//}
/**
* This is the full constructor.
*/
Products(int pid, String prodId, String prodType, String man,
String product, int qty, String price, Date dateAdded, boolean stockOnOrder) {
this.pid = pid;
this.prodId = prodId;
this.prodType = prodType;
this.man = man;
this.product = product;
this.qty = qty;
this.price = price;
this.dateAdded = dateAdded;
this.stockOnOrder = stockOnOrder;
}
public int getQty() {
return qty;
}
public void setQty(int qty){
this.qty = qty;
}
public int getPid() {
return pid;
}
public void setPid(int pid){
this.pid = pid;
}
public String getProdId() {
return prodId;
}
public void setProdId(String prodId){
this.prodId = prodId;
}
public String getProdType(){
return prodType;
}
public void setProdType(){
this.prodType = prodType;
}
public String getMan(){
return man;
}
public void setMan(String man){
this.man = man;
}
public String getProduct(){
return product;
}
public void setProduct(String product){
this.product = product;
}
public String getPrice(){
return price;
}
public void setPrice(String price){
this.price = price;
}
public Date getDateAdded() {
return dateAdded;
}
public void setDateAdded(Date dateAdded){
this.dateAdded = dateAdded;
}
public boolean getStockOnOrder() {
return stockOnOrder;
}
public void setStockOnOrder(boolean stockOnOrder){
this.stockOnOrder = stockOnOrder;
}
}
ActionUpdate.java
package elecstore;
import java.io.*;
import java.sql.*;
import java.text.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import elecstore.*;
import elecstore.UpdateBean;
public class ActionUpdate extends Action
{
public ActionForward perform(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
ActionErrors errors = null;
try {
UpdateBean update = new UpdateBean();
UpdateForm form = (UpdateForm)actionForm;
System.out.println(form.getProdId());
update.setProdId(form.getProdId());
System.out.println(form.getQty());
update.setQty(form.getQty());
System.out.println(form.getPrice());
update.setPrice(form.getPrice());
request.setAttribute("prodId",update.getProdId());
request.setAttribute("qty", new Integer(update.getQty()));
request.setAttribute("price",update.getPrice());
return mapping.findForward("update");
}
catch (NamingException ex) {
errors = new ActionErrors();
errors.add("database", new ActionError("errors.naming.error"));
}
catch (SQLException ex) {
errors = new ActionErrors();
errors.add("database", new ActionError("errors.database.error"));
}
catch (Exception e) {
System.out.println("AN OTHER" + e);
}
saveErrors(request,errors);
return mapping.findForward("error");
}
}
UpdateForm.java
package elecstore;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class UpdateForm extends ActionForm {
private String prodId;
private String price;
private int qty;
public void setProdId (String prodId) { this.prodId = prodId; }
public String getProdId () { return prodId; }
public void setPrice (String price) { this.price = price; }
public String getPrice () { return price; }
public void setQty (int qty) { this.qty = qty; }
public int getQty () { return qty; }
public void reset(ActionMapping mapping, HttpServletRequest request) {
prodId = null;
price = null;
qty = 0;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (prodId == null)
errors.add("prodId", new ActionError("errors.prodId.required"));
else if (prodId.length() == 0)
errors.add("prodId", new ActionError("errors.prodId.novalue"));
else if (price == null)
errors.add("price", new ActionError("errors.price.required"));
else if (price.length() == 0)
errors.add("price", new ActionError("errors.price.novalue"));
else if (qty == 0)
errors.add("qty", new ActionError("errors.qty.required"));
return errors;
}
}