?
Solved

Session Variables for Password Proteced Pages

Posted on 2004-11-07
3
Medium Priority
?
518 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:mderbin
3 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 1000 total points
ID: 12520112
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
 
LVL 1

Expert Comment

by:KartikShah
ID: 12521252
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
 
LVL 2

Expert Comment

by:arnon81
ID: 12526631
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
With more and more companies allowing their employees to work remotely, it begs the question: What are some of the security risks involved with remote employees and what actions should we take to secure them?
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question