Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Accessing the session object from a bean

Thread view: 
mills@brewersmarine.com - 13 Jan 2006 16:55 GMT
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.
Raymond DeCampo - 14 Jan 2006 18:38 GMT
> 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
>
[quoted text clipped - 9 lines]
> 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

Signature

This signature intentionally left blank.

mills@brewersmarine.com - 14 Jan 2006 22:06 GMT
>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
Raymond DeCampo - 15 Jan 2006 19:19 GMT
>>From a servlet... (more code)
>
[quoted text clipped - 44 lines]
> 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
Signature

This signature intentionally left blank.

mills@brewersmarine.com - 16 Jan 2006 14:47 GMT
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();
 }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.