Session Variables for Password Proteced Pages

I want to have a site that forces you to log in and then will not let you go to any pages unless you have entered a successful username and password (I am using an Oracle Db).  In PHP, I used Session variables, but I am new to JSP and do not know if it really has the same functionality.

I have a JSP book, but my attempts have failed so far.

How do I declare a session variable, and what do i have to do in order to keep the session alive and pass a variable along to each page so each page knows NOT to redirect the user back to the login if they have already done so successfully.

Any code or suggestions would be greatly appreciated.

Thanks,
MD
mderbinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ldbkuttyCommented:
A lots of codes in http://gotocode.com along with:
http://gotocode.com/apps.asp?app_id=12
Its about user registration, login information, etc..
you can view the online demo and download the source code

Some more:
http://www.jspin.com/func/search?qry=login&cat=all

< How do I declare a session variable, and what do i have to do in order to keep the session alive and pass a variable along to each page so each page knows NOT to redirect the user back to the login if they have already done so successfully. >

in page1.jsp:

<%
if(loginSuccess) {
   String name = request.getParameter( "username" );
   session.setAttribute( "theName", name );
}
%>

and in other pages you can retrieve it like:

<%=session.getAttribute("theName")%>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KartikShahCommented:
Hi,

JSP by default provides reference to the Session object associated to the request. the name of the variable is "session".

In order to access the information associated to the session you need to just call the
session.setAttribute("key","value") and
session.getAttribute("key")

ldbkutty has provided a good example for the same.

All you need to do, after a successful login, to set a attribute in the session object using the setAttribute method, and in all the required pages, you need to just retrieve the value from the session using the getAttribute method. if the getAttribute method returns null, then you can assume that the user as not logged ins, and hence forward him/her to the loging page.

Kartik

0
arnon81Commented:
How do I declare a session variable?
by default session is a available keyword in jsp page.
just do
session.setAttribute ("user", username); after your authentication process
ps: username is a String object containing the username

and what do i have to do in order to keep the session alive and pass a variable along to each page so each page knows NOT to redirect the user back to the login if they have already done so successfully.
just do this at the top of every jsp page,  that you wanted to secured, after imports statement
String username =(String) session.getAttribute("user");
if (username==null)
    response.sendRedirect ("login.jsp");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.