Click to See Complete Forum and Search --> : object instance


eu.rafael
09-26-2004, 04:12 PM
Hello again!
In a jsp page i have the source code bellow:
if(request.getParameter("obj")==null)
{ solti = new YYY.SolicitacaoTI(); }
With this object i full fill some fields of this page. In the first try, everything ok. But when i visit this page for the second time, the fields have the same information of the last time. Therefore, a new instance of this object itīs not created...why?
PS:
<jsp:useBean class="YYY.SolicitacaoTI" id="solti" scope="session"/>
PS2: Tomcat 5.0
I need the session scope to use the object in my page.
How to kill the session? I used first session.removeAttribute("object") and session.invalidate(), but when i visit the page for the second time, all the fields wasnīt in blank.... I think the object wasnīt removed.
Thankīs for your attention.

ray326
09-26-2004, 08:25 PM
First, that object is not a "parameter" if it's in the request session, it's an "attribute" so you should be using getAttribute() to retrieve it. Second, you've declared the bean to be in the session, not the request, so you can't access it with request.get* you use session.getAttribute(). Better yet since this is Tomcat 5 you can use all those handy JSTL custom tags to do what you're trying to do and keep the scriptlets to an absolute minimum.

eu.rafael
09-27-2004, 06:47 AM
Originally posted by eu.rafael
Hello again!
In a jsp page i have the source code bellow:
if(request.getParameter("obj")==null)
{ solti = new YYY.SolicitacaoTI(); }
With this object i full fill some fields of this page. In the first try, everything ok. But when i visit this page for the second time, the fields have the same information of the last time. Therefore, a new instance of this object itīs not created...why?
PS:
<jsp:useBean class="YYY.SolicitacaoTI" id="solti" scope="session"/>
PS2: Tomcat 5.0
I need the session scope to use the object in my page.
How to kill the session? I used first session.removeAttribute("object") and session.invalidate(), but when i visit the page for the second time, all the fields wasnīt in blank.... I think the object wasnīt removed.
Thankīs for your attention.

Ok, with the corrections, the line
if(session.getAttribute("solti")==null)
{ solti = new YYY.SolicitacaoTI(); }
itīs working out (off course).
But when a visit the page for the second time, a new instance itīs created.... when i full fill some fields one request send me to another page to execute one query to full fill some fields of the first page (i know that itīs not the best solution but itīs all i can do at the moment); when the request return to the first page, the old instance of the object itīs used by the container (the fieldīs that suppost to be with new informations, have only the old informations).
Thankīs for your attention.
Iīm sorry for my bad english.

ray326
09-27-2004, 10:31 AM
As long as you have an object referenced in the session it should be updateable by all JSPs and servlets in that session. You have to be careful, though, that you don't do something that creates a new copy of that object, leaving the original in the session reference. Sometimes this is difficult to see so it may be necessary to always renew the reference at the end of actions that manipulate it.

As far as your "old values" problem goes, that sounds suspiciously like browser caching. Make sure you send headers and/or use meta tags to disable caching on these JSPs.

eu.rafael
09-27-2004, 11:03 AM
Originally posted by ray326
As long as you have an object referenced in the session it should be updateable by all JSPs and servlets in that session. You have to be careful, though, that you don't do something that creates a new copy of that object, leaving the original in the session reference. Sometimes this is difficult to see so it may be necessary to always renew the reference at the end of actions that manipulate it.

As far as your "old values" problem goes, that sounds suspiciously like browser caching. Make sure you send headers and/or use meta tags to disable caching on these JSPs.

take a look on the reason of the session scope:

if(session.getAttribute("solti")==null)
{ solti = new YYY.SolicitacaoTI(); }
try{
if(quem.equals("1"))
{
dep = request.getParameter("dep");
nom = request.getParameter("nom");
if(!nom.equals("null"))
{
if(!nom.equals(""))
{
solicitacao = session.getAttribute("solti");
classe = solicitacao.getClass();
metodo = classe.getMethods()[getMetodo(classe.getMethods(),1)];
args[0] = new String(nom);
metodo.invoke(solicitacao,args);

// old values come back from here, the old object reference (i dont think thatīs a cache problem....)

centroc = classe.getField("centrocusto").get(solicitacao).toString();
tel = classe.getField("telefone").get(solicitacao).toString();
loc = classe.getField("local").get(solicitacao).toString();
locf = classe.getField("localf").get(solicitacao).toString();
ee = classe.getField("endeletronico").get(solicitacao).toString();
}
}

the page thatīs finish the userīs operation has

Object solicitacao = session.getAttribute("solti");
Class classe =solicitacao.getClass();
Method metodo = classe.getMethods()[getMetodo(classe.getMethods())];
Object[] args = new Object[]{request.getParameter("txtaDesc"), request.getParameter("selTipo"), request.getParameter("selOrigem"), request.getParameter("textData"), request.getParameter("showTime")};

metodo.invoke(solicitacao,args);
session.removeAttribute("solti");
session.invalidate();

Thatīs all what i do with this object.... i canīt figure out waht itīs happening...

Thankīs for your attention.

ray326
09-27-2004, 01:24 PM
One thing I notice is this.

if(!nom.equals("null"))
{
if(!nom.equals(""))
{
...

Do you REALLY want a String compare with "null" there? The way I normally handle this idiom is like this.

if (nom != null && !nom.equals(""))
{
...

eu.rafael
09-27-2004, 03:31 PM
You are right....