Hello,
I am having a hard time in passing a variable from the session to bean and
then display results using jsp/jstl.
I am working on the premise [any comments?] that labelServet.java sets values in the bean and stores the bean in the session.
The jsp/jstl then accesses the bean with the same name, and display the results
However, no results are displayed since values passed from the lableServlet are somehow not being stored or accessed properly.
Any input will be greatly appreciated.
Thanks,
Rauf
PS: The code follows.
===========
labelsTest.jsp
===========
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="labelHelper" class="com.hunza.kcw.LabelHelper">
<jsp:setProperty name="labelHelper" property="*"/>
</jsp:useBean>
<c:set var="allItems" value="${labelHelper.allItems}"/>
<table>
<c:forEach var="item" items="${allItems}">
<tr><td><c:out value="${item.itemId}"/></td>
<td><c:out value="${item.extendedPrice}"/></td>
<td><c:out value="${item.setsInUnit}"/></td>
</tr>
</c:forEach>
</table>
=============
labelsServlet.java
=============
package servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.sun.rowset.CachedRowSetImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hunza.kcw.SQLInterface;
import com.hunza.kcw.LabelHelper;
public class labelsServlet extends HttpServlet {
protected final Log logger = LogFactory.getLog(getClass());
public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
LabelHelper labelHelper = null;
try {
labelHelper = new LabelHelper();
} catch (SQLException ex) {
logger.info("**** SQL Exception caught*** ");
while (ex != null) {
logger.info("SQL State: " + ex.getSQLState());
logger.info("Message: " + ex.getMessage());
logger.info("Vendor: " + ex.getErrorCode());
ex = ex.getNextException();
}
}
labelHelper.setVendorId((Integer)session.getAttribute("VENDORID").intValue());
labelHelper.setShipmentId((Integer)session.getAttribute("SHIPMENTID".intValue()));
//labelHelper.setVendorId(13);
//labelHelper.setShipmentId(13);
logger.info("The vendor id is: " + labelHelper.getVendorId());
session.setAttribute("labelHelper",labelHelper);
gotoPage("/labelsTest.jsp", request, response);
}
private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
}
=============
LabelHelper.java
=============
package com.hunza.kcw;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.rowset.CachedRowSetImpl;
import com.hunza.kcw.SQLInterface;
import com.hunza.kcw.ItemBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//public class LabelHelper implements java.io.Serializable {
public class LabelHelper {
private SQLInterface iface = null;
private int vendorId, shipmentId;
protected final Log logger = LogFactory.getLog(getClass());
// Constructor
public LabelHelper() throws SQLException {
iface = new SQLInterface();
}
// =========================================================================
public List getAllItems () throws SQLException {
// From the CachedRowSet object, obtain item parameters and stuff into the List object.
List returnData = new ArrayList();
CachedRowSetImpl crs = null;
String query = "SELECT * FROM ";
try {
query += iface.getItemsTable();
query += " WHERE VENDORID = " + getVendorId() +
" AND SHIPMENTID = " + getShipmentId();
if (!iface.execQuery(query)) {
// exception caught, halt execution
logger.info("iface could not be initialized. HALTED!");
logger.info(query);
}
logger.info(query);
crs = iface.getNewCachedRowSet();
while (crs.next()) {
ItemBean item = new ItemBean();
item.setItemId(crs.getInt("ITEMID"));
item.setExtendedPrice(crs.getDouble("EXTENDEDPRICE"));
returnData.add(item);
}
} catch (SQLException ex) {
logger.info("**** SQL Exception caught *** ");
while (ex != null) {
logger.info("SQL State: " + ex.getSQLState());
logger.info("Message: " + ex.getMessage());
logger.info("Vendor: " + ex.getErrorCode());
ex = ex.getNextException();
}
}
return returnData;
}
public void setVendorId(int vendorId) { this.vendorId = vendorId; }
public void setShipmentId(int shipmentId) { this.shipmentId = shipmentId; }
public int getVendorId() { return this.vendorId; }
public int getShipmentId() { return this.shipmentId; }
}
=========
ItemBean.java
=========
package com.hunza.kcw;
public class ItemBean implements java.io.Serializable
{
private int vendorId, shipmentId, itemId, numberOfUnits, setsInUnit,
margin;
private String itemNumber, description, comments;
double unitCost, pricePerSet, extendedPrice;
public void setVendorId(int vendorId) { this.vendorId = vendorId; }
public void setShipmentId(int shipmentId) { this.shipmentId = shipmentId; }
public void setItemId(int itemId) { this.itemId = itemId; }
public void setItemNumber(String itemNumber) { this.itemNumber = itemNumber; }
public void setNumberOfUnits(int numberOfUnits) { this.numberOfUnits = numberOfUnits; }
public void setSetsInUnit(int setsInUnit) { this.setsInUnit = setsInUnit; }
public void setDescription(String description) { this.description = description; }
public void setUnitCost(double unitCost) {this.unitCost = unitCost; }
public void setMargin(int margin) { this.margin = margin; }
public void setPricePerSet(double pricePerSet) {this.pricePerSet = pricePerSet; }
public void setExtendedPrice(double extendedPrice) {this.extendedPrice = extendedPrice; }
public void setComments(String comments) {this.comments = comments; }
public int getVendorId() { return this.vendorId; }
public int getShipmentId() { return this.shipmentId; }
public int getItemId() { return this.itemId; }
public String getItemNumber() { return this.itemNumber; }
public int getNumberOfUnits() { return this.numberOfUnits; }
public int getSetsInUnit() { return this.setsInUnit; }
public String getDescription() { return this.description; }
public double getUnitCost() { return this.unitCost; }
public int getMargin() { return this.margin; }
public double getPricePerSet() { return this.pricePerSet; }
public double getExtendedPrice() { return this.extendedPrice; }
public String getComments() { return this.comments; }
}
=========
SQLInterface.java
===========
package com.hunza.kcw;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.*;
public class SQLInterface {
/* The SQLInterface class defines the folllowing:
1. Database Driver
2. Database URL
3. Tables names to tbe used in the application, through getters.
It also opens a connection to the database, creates a CachedRowSet object and closes the connection.
*/
private String dbDriver;
private String dbURL;
private ResultSet rs;
private CachedRowSetImpl crs;
private String vendors = "KCWVENDORS";
private String shipments = "KCWSHIPMENTS";
private String items = "KCWITEMS";
private String users = "KCWUSERS";
public SQLInterface() {
/* Constructor - Define the database related variables here.
*/
dbDriver = "com.mysql.jdbc.Driver";
dbURL = "jdbc:mysql://localhost/test";
// For the online database, use the following URL
// dbURL = "jdbc:mysql://localhost/mydb?user=xy&password=xyz";
}
public boolean execQuery(String query) {
Statement stmt = null;
Connection conn = null;
try {
Class.forName(dbDriver).newInstance();
conn = DriverManager.getConnection(dbURL);
stmt = conn.createStatement();
//stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(query);
// create CachedRowSet and populate with the resultset
crs = new CachedRowSetImpl();
crs.populate(rs);
// Close the connection.
conn.close();
return true;
} catch (SQLException ex) {
System.out.println("**** SQL Exception caught in the SQLInterface class*** ");
while (ex != null) {
System.out.println("SQL State: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor: " + ex.getErrorCode());
ex = ex.getNextException();
}
return false;
} catch (Exception e) {
System.out.println("**** Exception caught in SQLInterface*** " + e.getMessage());
return false;
}
}
public boolean execUpdate(String query) {
Statement stmt = null;
Connection conn = null;
try {
Class.forName(dbDriver).newInstance();
conn = DriverManager.getConnection(dbURL);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.execute(query);
conn.close();
return true;
} catch (SQLException ex) {
System.out.println("**** SQL Exception caught *** ");
while (ex != null) {
System.out.println("SQL State: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor: " + ex.getErrorCode());
ex = ex.getNextException();
}
return false;
} catch (Exception e) {
System.out.println("**** Exception caught*** " + e.getMessage());
return false;
}
}
public ResultSet getNewResultSet() {
return rs;
}
public CachedRowSetImpl getNewCachedRowSet() {
return crs;
}
public boolean commitToDatabase(CachedRowSetImpl crs) {
Connection conn = null;
try {
Class.forName(dbDriver).newInstance();
conn = DriverManager.getConnection(dbURL);
// propagate changes and close connection
crs.acceptChanges(conn);
conn.close();
return true;
} catch (SQLException ex) {
System.out.println("**** SQL Exception caught *** ");
while (ex != null) {
System.out.println("SQL State: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor: " + ex.getErrorCode());
ex = ex.getNextException();
}
return false;
} catch (Exception e) {
System.out.println("**** Exception caught*** " + e.getMessage());
return false;
}
}
// Getters for the table names
public String getVendorsTable(){
return vendors;
}
public String getShipmentsTable() {
return shipments;
}
public String getItemsTable() {
return items;
}
public String getUsersTable() {
return users;
}
}