| Author |
session
|
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
i am developing a web application using struts 1.3
my heirarchy is like this
userlogin.jsp calls admin.jsp in which one of the frame call left.jsp which has a link changepassword.jsp
i have define a sesion on my userlogin.jsp
<%
session = request.getSession(false);
String user= (String)session.getAttribute("username");
session.setAttribute("username",user);
%>
in my all pages i have call
<%String username=request.getParameter("un"); %>
<%=un%>
but this is printing null i have also defined these lines in my all jsp but still it is giving me null
please help me out with sessions if there is any error then also please help me because i donot know much about session
thank you
|
 |
Shailesh Narkhede
Ranch Hand
Joined: Jul 10, 2008
Posts: 356
|
|
Hi Aditi,
Are you appending "un" parameter and its value in every time while submitting form.
bye the way you are putting username in session attribute with name "username" nad you are trying to get it from request paramter you can get it from session.
HTH
|
Thanks,
Shailesh
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
thank you Shailesh for your reply
but please help me more by telling me how to do it thru sessions only as i am new to it & i also cannot foun it on google
i am not appneding un parameter again & again
thank you
looking forward for your answer
|
 |
Shailesh Narkhede
Ranch Hand
Joined: Jul 10, 2008
Posts: 356
|
|
Hi Aditi,
you can get session object from request you can see HttpServletRequest Object help for that.
After getting session you can getAttribute from session and show it on each JSP.
|
 |
debraj mallick
Ranch Hand
Joined: Mar 08, 2011
Posts: 188
|
|
Hi Aditi,
if you use
then you can get the username as
in all jsp pages, but you have used
String username=request.getParameter("username");
there is difference between request and session
I think you understand what i mean to say........
|
 |
debraj mallick
Ranch Hand
Joined: Mar 08, 2011
Posts: 188
|
|
hi,
i hope this link will help you to understand better (some examples are there)
http://www.java2s.com/Code/Java/JSP/Session.htm
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
thanks once agin debraj you again saved me
now i understand what you are saying
thanks to you too Shailesh you have also helped me alot
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
hey debraj i have tried
String user= session.getattribute(valid);
but its also giving me null please tell what to do
thank you
|
 |
debraj mallick
Ranch Hand
Joined: Mar 08, 2011
Posts: 188
|
|
hi,
String user= session.getattribute(valid);
it should be
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
hi debraj
i have written same i have written it wrong here only i am sorry for that
String user = (String) session.getAttribute("valid");
if(user==null)return mapping.findForward("failure");
& its taking nnull value only not showing me success
what should i do
|
 |
debraj mallick
Ranch Hand
Joined: Mar 08, 2011
Posts: 188
|
|
hi,
i didn't find anything wrong
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8291
|
|
The code in a JSP is run before the JSP is displayed on the browser. If you are expecting to capture the username from the JSP where a user enters it, you will be disappointed. What you want to do is submit the login form to an action and let the action set the username in the session.
I recommend that you spend some time working through the many Struts tutorials (see our Struts FAQ before trying your own web app. A little research up front will save you many headaches down the road.
And use Struts 2 if you can help it.
|
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
thanks debraj
thanks joe for telling me that i should set session in my action form not in my jsp page thank you once again
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
hey joe
now i have set my userLoginAction
& my changepasswd.jsp as
& my changepasswdAction as
One more thing i would kike to ask you that after userlogin.jsp->admin.jsp->left.jsp->changepasswd.jsp is a link with target set on right.jsp
do i need to start a session in all in betweeen files too or its not neccessary
thank you
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8291
|
|
Can you explain what each of those lines in your code does? I don't think they do what you think they do.
A session is "started" once. You only need to reference it when you need to get/set an attribute.
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
tually firstly a session is started
then the
String user1=userLoginForm.getUsername().trim();
stores name of user who is logged in from my userloginform
String user= (String)session.getAttribute(user1);
assigsn user a value
session.setAttribute("user",user);
this value is to be used i think it does this
if i am not mistaking it
if any error please guide me to correct way
thanks
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8291
|
|
Aditi agarwal wrote:
String user= (String)session.getAttribute(user1);
assigsn user a value
What value do you expect here?
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
|
hey joe in user i need th value of person who is logged in fom my page userlogin.jsp whose form is userloginform
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8291
|
|
Isn't that what the previous line does?
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
thats what i have written but you said it might be correct or ....
so i just ask you then tell me
you told me on jsp we cant track the user so on action i have written these lines but still me getting null can you just help me why still its null
code on action page
please help me
thank you
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
hey one more thing i noticed my code
my user1 is printing 'admin' which is my user but my user is printing null
help me joe
thank you
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
hey some body please help me i need it yr
please
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8291
|
|
Please be patient. We are all volunteers here, answering questions in our free time.
You say:
my user1 is printing 'admin' which is my user but my user is printing null
So doesn't the variable "user1" contain what you are looking for?
Why do you attempt to retrieve something out of the session before you set it in the session?
Again, I will ask you to have a look at our FAQ and go through some tutorials (perhaps even the Java Tutorial). It is much easier to first gain an understanding of the language and framework first than it is to cobble together an application without understanding either.
|
 |
krishna bala
Ranch Hand
Joined: Jul 20, 2009
Posts: 48
|
|
use the following code in your action class
String userName = <yourform reference variable>.getUserName();
session.setAttribute("username",userName) ;
where ever you want username from session, just use the following code
String userName = (String)session.getAttribute("username");
note : donot create session object on jsp page, because session object is an implecit object.
|
 |
Aditi agarwal
Ranch Hand
Joined: Feb 23, 2011
Posts: 225
|
|
thank you joe & krishna
& sorry too joe for being impatient actually i need it bcause only one module of my project is left behind all is complete else so i need it fast
lemme try your ideas
|
 |
 |
|
|
subject: session
|
|
|