Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-06-2004, 03:11 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
check if session variable is null?

how the heck do i check if a session variable is null?

here is the code doing the work
Code:
// login.jsp: set session var
User u = new User("sde");
session.putValue("user",u);
Code:
// header.jsp: make sure session user is valid
// this always evaluates to true
if( session.getValue("u") != null ){
  User u = (User)session.getValue("user");
}else{
  response.sendRedirect("../login.jsp");
}
Code:
// logout.jsp
session.invalidate();
after i logout, and go to any page that uses that header file, i can do: out.println(session.getValue("u"), and it prints "null"

i am not using that header file in the login or logout page.
__________________
Mike
sde is offline   Reply With Quote
Old 07-06-2004, 06:20 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,151
Belisarius is on a distinguished road
I use the jsp:useBean tag, but what you're doing in principle should work. I'm a bit confused by "session.getValue("u")", as you did a "session.putValue("user",u)", so where the "u" comes from I'm not sure.

Here's how I do it.

Code:
<jsp:useBean id="user" class="dec.common.beans.UserBean" scope="session" />
<jsp:useBean id="error" class="dec.common.beans.ErrorBean" scope="session" />

<%
  String host = request.getServerName();
  String path = request.getRequestURI();
  if(user == null){
%>
  <jsp:forward page="/index.jsp" />
<%
  }else if(user.getUser() == null){
%>
  <jsp:forward page="/index.jsp" />
<%
  }else if(!user.getAuth()){
%>
  <jsp:forward page="/index.jsp" />
<%
  }else if(!request.isSecure()){
    response.sendRedirect(response.encodeRedirectURL("https://" + host + path))
  }
%>
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-07-2004, 11:07 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
thanks again. i haven't been using tags as much as i probably should.

is using tags just good practice and meant to keep your code clean? or is there a performance difference too?
__________________
Mike
sde is offline   Reply With Quote
Old 07-07-2004, 12:27 PM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,151
Belisarius is on a distinguished road
Tags are meant to be a restriction on the coder in an attempt to keep proper MVC seperation. I'm not aware of any performance difference, it's just that there are things you can do with tags at times that it's hard to emulate otherwise. For instance, the jsp:forward tag will keep the requested document in the address bar, whereas the redirect the address will change. The reason being that the jsp:forward is actually the same jsp as requested, it's just that further processing is forwarded to another jsp, whereas the redirect prompts the client to request a new document.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP N00b - How to check if a variable is set? or empty? helz MS Technologies ( ASP, VB, C#, .NET ) 3 11-04-2004 10:19 AM
Changing variable type for edit box : mfc - .net sde Standard C, C++ 0 01-31-2003 09:53 AM


All times are GMT -8. The time now is 10:13 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting