aspose file tools*
The moose likes Servlets and the fly likes How can I save my variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can I save my variable" Watch "How can I save my variable" New topic
Author

How can I save my variable

qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
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?


Thanks...qingwu<br />When I open my eyes,I see your pretty face.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
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

Joined: Feb 19, 2003
Posts: 147
Sorry,I only use jsp,servlet and javabean without using Ejb.to this,can it be sloved?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
It's very nice of you,I will try!
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
how about set a Servlet Global Variable.
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
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

Joined: Feb 19, 2003
Posts: 147
I doubt if Array can be used in 'setAttribute("name",value)' as 'value'
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
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 ]

Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
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

Joined: Feb 19, 2003
Posts: 147
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

Joined: Feb 19, 2003
Posts: 147
Thanks a lot.
My question had been solved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I save my variable