/    Sign up×
Community /Pin to ProfileBookmark

object instance

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.

to post a comment
Java

6 Comments(s)

Copy linkTweet thisAlerts:
@ray326Sep 27.2004 — 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.
Copy linkTweet thisAlerts:
@eu_rafaelauthorSep 27.2004 — [i]Originally posted by eu.rafael [/i]

[B]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. [/B]
[/QUOTE]


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.
Copy linkTweet thisAlerts:
@ray326Sep 27.2004 — 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.
Copy linkTweet thisAlerts:
@eu_rafaelauthorSep 27.2004 — [i]Originally posted by ray326 [/i]

[B]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. [/B]
[/QUOTE]


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.
Copy linkTweet thisAlerts:
@ray326Sep 27.2004 — One thing I notice is this.
<i>
</i>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.
<i>
</i>if (nom != null &amp;&amp; !nom.equals(""))
{
...
Copy linkTweet thisAlerts:
@eu_rafaelauthorSep 27.2004 — You are right....
×

Success!

Help @eu_rafael spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...