Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I save my variable

 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone:
How can I save my variables in the servlet,I want to use them followed serveral pages.Util I visite this page again,variables value are saved again.If I can use bean,can I setValue in one page and getValue in the other page?
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by qingw wang:
Hello everyone:
How can I save my variables in the servlet,I want to use them followed serveral pages.Util I visite this page again,variables value are saved again.If I can use bean,can I setValue in one page and getValue in the other page?


i think you can use entity bean in this case.
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,I only use jsp,servlet and javabean without using Ejb.to this,can it be sloved?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by qingw wang:
Hello everyone:
How can I save my variables in the servlet,I want to use them followed serveral pages.Util I visite this page again,variables value are saved again.If I can use bean,can I setValue in one page and getValue in the other page?

Yes..you can use a javabean with proper getters and setter methods, and specify the scope of your bean according to your needs.
you can also put it in the session object.

another option is to put your *variables* in the servletcontext:

hope this helps
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very nice of you,I will try!
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about set a Servlet Global Variable.
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I set Application variable as array.
ServletContext sctx = getServletContext(); String[] x={"FirstSTRING","The second string"};
sctx.setAttribute("retvalue",x);
then I will how to get my data by getAttribute.
I try to do this in serlvet:
String[] temp=sctx.getAttribute("retvalue");
for(int i=0;i<x.length;i++){
....
but I cann't get it.why?
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt if Array can be used in 'setAttribute("name",value)' as 'value'
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by qingw wang:

String[] temp=sctx.getAttribute("retvalue");

I think you are neglecting to cast the object ref:

Originally posted by qingw wang:
Util I visite this page again,variables value are saved again.If I can use bean,can I setValue in one page and getValue in the other page?

I think we all assumed that you intend to reuse the variables/data during the same session, but could you confirm this? If you want data persistance from one session to another, then depending on the data you may have to involve the use of a database, and some way of tracking user identity...
This of course can be done without EJB's, but is slightly more complex than maintaining session variables.
Just to reiterate what the others have said concerning session vs. context attributes, you need to distinguish between values that are unique to a visitor (session scope), and those that are global to your web application (application scope).
[ May 28, 2003: Message edited by: Philip Shanks ]
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get data by parse xml string.
Stirng of xmldocument which is from DatagramPacket by UDP.
I want to save variables of being parsed for next jsp or servlet page.
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate for Everybody's help,Thank you very much.but I still feel puzzle a little.
We suppose that
there are some variables in one servlet,I want to use them in the following jsp page.When I return to servlet page,Variables are evaluated again.
I try to do so
(1)If using ServletContext,Variables will keep the same value as I visited last time,It is not my expectation
(2)If using HttpSession,I try to do:
String[] abc={"avd","sfdf","sfsd","ff","ddfd"};
HttpSession hs=req.getSession(true);
hs.setAttribute("funcp",abc);
if I want to get them in the forward jsp page,What should I do?
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
My question had been solved.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic