Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating session using JSP 2

Status
Not open for further replies.

jass1220

Programmer
May 20, 2003
88
AE
how i can create session using jsp code ...

this not working!

session("user") = strUser;


thanks
 
In JSP, the "session" variable is a HttpSession object which is created by the servlet container that you are using (ie Tomcat or Resin etc). You cannot "create" a session object yourself in the way you are trying.

If you tell us exactly what you are trying to do, maybe we can help ...
 

should help out

_____________________________________________________________________
onpnt2.gif
[sub]
The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
[/sub]
 
hi again

session.setAttribute( "theName", name );

what is the ( "theName", name ); ??

where should the value be .. and where is the session name ?

 
from the tutorial shown here
<%
String name = request.getParameter( &quot;username&quot; );
session.setAttribute( &quot;theName&quot;, name );
%>


the value you want to hold in the session object is the second parameter. the actual name that you reference is the parameter shown in quotes
so if you had username as a form field and want to hold this value you would make that bit a bit mroe understandable as
if teh user enter &quot;john doe&quot;
<%
String username= request.getParameter( &quot;username&quot; );
session.setAttribute( &quot;currentUsername&quot;, username);
%>
now currentUsername holds john doe
and retrieve the value as
session.getAttribute( &quot;currentUsername&quot; )
that would output &quot;john doe&quot;

_____________________________________________________________________
onpnt2.gif
[sub]
The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
[/sub]
 
As I said before, the &quot;session&quot; object is a HttpSession object provided by the servlet conatiner.

You can allocate variables to this session object which are stored on the server like so :

Code:
String myuser = &quot;Dave&quot;;
session.setAttribute(&quot;myUserName&quot;, myUser);

out.println(session.getAttribute(&quot;myUserName&quot;);

So this allocates a String object, called &quot;myuser&quot; to a session object named &quot;myUserName&quot; - which can then be altered or used using the getAttribute() or setAttribute() methods of the HttpSession object ....
 
sessions are working perfectly

now .. if am using teh session in other pages ..
i want to give the variable strUser teh value of the session ... how i can do it ?

i tried this:

String strUser = session.setAttribute( &quot;userName&quot; );

and tried this:

String strUser = session.getAttribute( &quot;userName&quot; );

but none of them working

 
Hi,

1. session.setAttribute(String, Object); you need set the Object with some name.

Ex: session.setAttribute(&quot;userName&quot;, &quot;someName&quot;);

2. session.getAttribute(&quot;&quot;) returns you object. you need to typecaste it to String in your case.

Ex:

String strUser = session.getAttribute(&quot;userName&quot;).toString();

or

String strUser = (String)session.getAttribute(&quot;userName&quot;);

Cheers,
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top