Accessing the session object from a bean

M

mills

I have been working on the following bean for several days and not
getting very far. I think I am missing something but my java knowledge
is limited.


Here are the relivent pieces of code...


package com.mysoftware.poorderItem;


import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;


public class poorderItemBean implements
HttpSessionBindingListener,Serializable {
private boolean hasError;
private boolean needsConfirm;
private String rawItem;
private String evalItem;
private String Desc1;
private String Desc2;
private String driver = "net.sourceforge.jtds.jdbc.Driver";
private String DBIPAddr = "";
private String url = "jdbc:jtds:sqlserver:";
private String user;
private String pass;
private Connection con;
private HttpSession session;
private ServletContext sc;


public poorderItemBean() {
initValues();
}


private void initValues() {
hasError = true;
needsConfirm = false;
rawItem = null;
evalItem = null;
Desc1 = null;
Desc2 = null;
lineid = -1;
}


private void initDBstuff() {
DBIPAddr = sc.getInitParameter("DatabaseIPAddr");
user = sc.getInitParameter("DatabaseUser");
pass = sc.getInitParameter("DatabasePassword");
url = url + DBIPAddr.trim();
}


public void valueBound(HttpSessionBindingEvent event) {
session = event.getSession();
sc = session.getServletContext();
initDBstuff();
}


public void valueUnbound(HttpSessionBindingEvent event) {
session = null;
sc = null;
}


public String getRawPartNumber() {
return rawItem;
}


public String getPartNumber() {
return evalItem;
}


public void setPartNumber (String pn) {
String tempPartNumber = null;


if (!pn.trim().equals(this.evalItem.trim())) {
// Lookup new description and price


try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,pass);
CallableStatement cs = con.prepareCall("{call EvalCustPOLine
?,?}");
CallableStatement cs1 = con.prepareCall("{call HuntforCatpart
?}");
PreparedStatement ps1 = con.prepareStatement("Select in_Catpart
from dbo.Inventory where in_Catpart = ? and in_Delete=0");
PreparedStatement ps2 = con.prepareStatement("Select in_Catpart,
in_Desc, in_Desc2, in_Nonstock from dbo.Inventory where in_Catpart = ?
and in_Delete=0");


ResultSet rsInvent1;
ResultSet rsInvent2;
ResultSet rsEval;
ResultSet rsHunt;


ps1.setString(1,pn.trim());
rsInvent1 = ps1.executeQuery();


while (rsInvent1.next()) {
tempPartNumber = rsInvent1.getString("in_Catpart").trim();
}


rsInvent1.close();
ps1.close();


if (tempPartNumber==null) {
//Hunt for part number


this.hasError = true;


cs1.setString(1,pn.trim());
rsHunt = cs1.executeQuery();


while(rsHunt.next()) {
tempPartNumber = rsHunt.getString("Suggestion").trim();
if (rsHunt.getInt("Matches")==1) {
this.needsConfirm = true;
this.hasError = false;
} else {
this.needsConfirm = false;
this.hasError = true;
}
}


rsHunt.close();
cs1.close();


}


//Load Description and price


this.evalItem = null;
this.Desc1 = null;
this.Desc2 = null;
this.IsSpecialorder = false;


if (tempPartNumber!=null) {
ps2.setString(1,tempPartNumber);
rsInvent2 = ps2.executeQuery();


while (rsInvent2.next()) {
this.evalItem = rsInvent2.getString("in_Catpart").trim();
this.Desc1 = rsInvent2.getString("in_Desc").trim();
this.Desc2 = rsInvent2.getString("in_Desc2").trim();
this.IsSpecialorder = rsInvent2.getBoolean("in_NonStock");
}


rsInvent2.close();
ps2.close();


}


} catch (Exception e) {
sc.log("Exception caught (poorderItemBean) : "+e.getMessage());
}
}
}


When I call setPartNumber() I get an exception. It looks like it hasn't

assigned a session or servletcontext yet. I am trying to get the
database connection information from the web.xml file.


Any help/suggestions?


Thanks,
Tim


P.S. Sorry if this appears as a cross post. I posted in
comp.lang.java.beans a few days ago and there doesn't seem to be any
activity there.
 
R

Raymond DeCampo

When I call setPartNumber() I get an exception.

From where? How? (E.g. a servlet, a JSP, an applet, an application,
etc.) What is the exact exception?
It looks like it hasn't

assigned a session or servletcontext yet. I am trying to get the
database connection information from the web.xml file.


Any help/suggestions?


Thanks,
Tim


P.S. Sorry if this appears as a cross post. I posted in
comp.lang.java.beans a few days ago and there doesn't seem to be any
activity there.

This would be a multi-post in that case, not a cross-post. Under the
circumstances, a forgivable one.

HTH,
Ray
 
M

mills

From a servlet... (more code)

public class ProcessPOXLS extends BaseAppServlet {

public void doProcessRequest (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

ServletContext sc = getServletContext();
HttpSession session = request.getSession();

com.mysoftware.poorderItem.poorderItemBean ItemBean;

try {

List poList = (List)session.getAttribute("poList");

if (poList==null) {
poList = new ArrayList();
}

int quarter = (maxRows-1) / 4;

for (int i = 1; i <= (maxRows-1);i++) {

ItemBean = new com.mysoftware.poorderItem.poorderItemBean();

ItemBean.setLineID(i);

ItemBean.setHasError(false);

ItemBean.setPartNumber('5023');

poList.add(ItemBean);

}
session.setAttribute("poList",poList);

}
catch (Exception e) {
sc.log("Exception caught (ProcessXLS) : "+e.getMessage());
}
}

All I get is the following in my tomcat log file :

2006-01-11 17:10:13 StandardContext[]Exception caught (ProcessXLS) :
null

I have put in sc.log statements to help trace how far this gets and it
fails immediately after ItemBean.setPartNumber
 
R

Raymond DeCampo

From a servlet... (more code)

public class ProcessPOXLS extends BaseAppServlet {

public void doProcessRequest (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

ServletContext sc = getServletContext();
HttpSession session = request.getSession();

com.mysoftware.poorderItem.poorderItemBean ItemBean;

try {

List poList = (List)session.getAttribute("poList");

if (poList==null) {
poList = new ArrayList();
}

int quarter = (maxRows-1) / 4;

for (int i = 1; i <= (maxRows-1);i++) {

ItemBean = new com.mysoftware.poorderItem.poorderItemBean();

ItemBean.setLineID(i);

ItemBean.setHasError(false);

ItemBean.setPartNumber('5023');

poList.add(ItemBean);

}
session.setAttribute("poList",poList);

}
catch (Exception e) {
sc.log("Exception caught (ProcessXLS) : "+e.getMessage());
}
}

All I get is the following in my tomcat log file :

2006-01-11 17:10:13 StandardContext[]Exception caught (ProcessXLS) :
null

Log the stack strace of your exception as well via

sc.log("Exception caught (ProcessXLS) : " + e.getMessage(), e);

This should tell you exactly what line is failing (assuming you have
compiled with debug information).

Ray
 
M

mills

Thank you for the correction to my sc.log code. I can now see exactly
where this line is failing.

It is failing on the following line...

sc.log('Some tracing information here');

I believe that the ServletContext object sc is NULL. It gets
initialized when valueBound(HttpSessionBindingEvent event) is called. I
don't think that this event is getting called/triggered. I am not sure
if I have implemented HttpSessionBindingListener properly.

Here is the code for the valueBound event...

private HttpSession session;
private ServletContext sc;

public void valueBound(HttpSessionBindingEvent event) {
session = event.getSession();
sc = session.getServletContext();
initDBstuff();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top