Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Accessing the session object from a bean

Reply
Thread Tools

Accessing the session object from a bean

 
 
mills@brewersmarine.com
Guest
Posts: n/a
 
      01-13-2006
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.

 
Reply With Quote
 
 
 
 
Raymond DeCampo
Guest
Posts: n/a
 
      01-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> 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

--
This signature intentionally left blank.
 
Reply With Quote
 
 
 
 
mills@brewersmarine.com
Guest
Posts: n/a
 
      01-14-2006
>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

 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      01-15-2006
(E-Mail Removed) wrote:
>>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
--
This signature intentionally left blank.
 
Reply With Quote
 
mills@brewersmarine.com
Guest
Posts: n/a
 
      01-16-2006
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();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving request scope bean reference to session scone bean gallas.robert@gmail.com Java 1 03-19-2008 01:09 PM
Modelling an object as Session Bean or Entity Bean ? Francesco Java 3 12-07-2006 04:22 PM
[ejb][jboss]Bean inside a serialized object in another bean carlg@libertybasic.com Java 1 09-13-2006 08:10 PM
EJB Transaction (Session Bean - Entitiy Bean) X3 Java 0 10-01-2004 07:41 AM
can a session bean return a local entity bean object? David Thielen Java 2 09-12-2003 07:45 AM



Advertisments