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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jps and Session

Status
Not open for further replies.

Khanjan

Programmer
Feb 24, 2004
41
NL
Hi everyone,

I am developing a CMS(Content Management System). I need to know how i can make sessions in jsp. After creating the Session in a login page the data like username should added in the session.In another page , addArtikel.jsp, i want to contoll if someone is logged on or not, other wise he may not use this page.

How should i do this contoll in addartikel, when i have made this in the login page :
HttpSession s = request.getSession();
s.setAttribute("naam", Username);
 
if (session.getAttribute("naam") == null) {
// not logged in
} else {
// already logged in
}

Ideally though this should be done from a database procedure call.
 
"Ideally though this should be done from a database procedure call."

What do you mean with database procedure?

"if (session.getAttribute("naam") == null) {
// not logged in
} else {
// already logged in
}"

Should i put this code in the login.jsp or in artikel.jsp.

I want to prevent people to access artikel.jsp by typing the URL of it. The user must be logdin, other wise he/she should see the login page.

This is what i want to achive. If you give comment, please give me some code.

Thanx
 
Hi,

Copy the above code into artikel.jsp page.

if (session.getAttribute("naam") == null) {
// not logged in
response.sendRedirect("login.jsp");
} else {
// already logged in
}

Cheers,
Venu
 
By "database procedure call" I mean that you should really log all login-sessions into a database in order to validate the incoming user, and to verify whether they are logged in for later pages. While session objects should not be lost, my live experience is that it can happen, and so you should not rely on a session object alone to determine whether a user is logged in or not.

One way to reuse code for determing whether a use is logged in or not is to use a taglib to do it. This way, you write the validating code only once, but can call it many times rom many pages.

For each *protected* page in your website you should check whether the user is logged in. Using taglibs enables this to be simply. Check out for the tutorials on JSP and taglibs.
 
Thank you for your replyes.

<%
if (session.getAttribute("naam") == null || session.getAttribute("passwordAdmin")==null)
{
response.sendRedirect("login.jsp");
}
else
{ %>

You are logged in as:
<%= session.getAttribute("naam") %>
<% }%>

I am puting the above code in to Artikel.jsp.
When i am typing the URL : Localhost:8080/artikel.jsp.
I can access this page, even i if i havent logged in.
When i am closing the internet Explorer, and coming back to artikel.jsp, i am alowed to come there, Why?

<%= session.getAttribute("naam") %> this action gets the user name that i have put in a session in the login page. I am getting this name, even if i havent logged in, Why..?

 
In Login.jsp page after controlling the password and username i am doing this:

HttpSession s = request.getSession(true);
s.setAttribute("naam", strCheckUsername);
s.setAttribute("passwordAdmin", strCheckPassword);
s.setAttribute("loggedin", "true");

And in the Artikel.jps

String loggedin=(String)session.getAttribute("loggedin");

if (loggedin!="true")
{
response.sendRedirect("niet.jsp");
}

else
{ %>

You are logged in as:
<%= session.getAttribute("naam") %>
<% }%>

this works only once.
When i am logged with a name and try artikel.jsp it works.
But when i am coming back with another username logged in, the artikel.jsp shows the old username, Why?

Please help me,
 
You can not use unary operators to test for equality of String objects :

if (loggedin!="true")
{
response.sendRedirect("niet.jsp");
}


This should be :

Code:
  if (loggedin == null || !loggedin.equals("true")) {
    response.sendRedirect("niet.jsp");
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top