• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to keep some variable in different Jsp Pages

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I user session to keep some variable in order to be used
in later different page.
when I use relative url to indirect another page, the session
become null.
for ex:
the A.jsp
<%
String userID = "aaa";
session.setAttribute("username", userID);
String myuserID = (String)session.getValue("username");
%>
User ID: <%=myuserID%><br>
UserJenny ID: <%=userID%><br>
<!--a href="testSessionB.jsp">All </a-->
<jsp:forward page="testSessionB.jsp"/>

the B.jsp
<%
String userID = (String)session.getValue("username");
%>
SessionUser ID: <%=userID%>

in A.jsp when I use
<a href="testSessionB.jsp">All </a>
the SessionUserID is null.
when I use
<jsp:forward page="testSessionB.jsp"/>
the SessionUserID is aaa.
So, what's the differents? and how can I keep my
variable for all the application?
Thanks
Krussi
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Krussi,
maybe it's the easy way, if you implement a JavaBean in your page. You write and compile a JavaBean with the usual set and get methods to store your variables in it.
Then you set the Bean's scope to "session".
Here a code example:

To access the values in your JSP-Code, do something like this:

I think you should try it.
Greetings
H.-Gerd
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just tried your code on my Tomcat 3.1 server and it worked fine. One note: I'd suggest using getAttribute() instead of getValue() in your b.jsp
bear
 
krussi rong
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everybody!
I want to use session keep some variables.
but the problem is when I use the local tomcat
the top code works fine, when I use the server's
tomcat,it didn't work. Is there anything to
do with the tomcat's configration?
thanks
Krussi
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i guess that is some server problem as session means it has to be same for the entire user session regardless of pages he/she visits...
of course JavaBean use is the best i would suggest as i do the same everytime to do session logout/login management for the users...its an elegant way i guess to just have a session level bean storing username/passwd and thats it! u r done.
regards
maulin.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic