aspose file tools*
The moose likes JSP and the fly likes Showing extra header/feature for MEMBER and GUEST Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Showing extra header/feature for MEMBER and GUEST" Watch "Showing extra header/feature for MEMBER and GUEST" New topic
Author

Showing extra header/feature for MEMBER and GUEST

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
First off let me introduce my self
I am a new Servlet programmer, to be at least 2 months in learning Java Servlet JSP.
I am in love with Java Servlet

So I face this problem. I want a single JSP page that can tell if the user is a MEMBER can GUEST.

So for example when the user clicked on a Link to IndexJSP page, the page can tell if he/she is a GUEST or a MEMBER.
The GUEST and MEMBER page are similar except MEMBER page has more feature and the login/signup button is not show up.
The GUEST page is the opposite of the member page, the Login/signup button is show up.

The code I show below produced an error
org.apache.jasper.JasperException: An exception occurred processing JSP page /Indexjsp.jsp at line 19

16: </head>
17: <body>
18: <h1>Hello World!</h1>
19: <c:when test="${bool == '1'}">
20: <jsp:include page="/Header/LogedIn_Header.jsp"></jsp:include>
21: </c:when>
22: <ctherwise>

So the code i've been trying to do recently was

The a href will call a servlet called Index. The servlet will check if the session exists

So now I will show you what i've done so far with the IndexServlet code


I think the servlet code above is correct. What it does is that I hope it will read whether the session is already exists or not.
If the session is exists. It will set some attributes
- bool, this contains a number for the JSP page to check if the user already has a session or not.

Then the response will be sendRedirect to the Indexjsp.jsp page.

The response will be desiplayed in the Indexjsp.jsp page


In the JSP code, I wrote if the bool is 1 (means that the users has a session already/logged in) therefore we will put an appropriate member header for them.
Else we must put an appropriate header for the Guest where the header shows the Login/Signup button

Does any one could give me a solution for this problem please.
Perhaps another way which could give me the same result as I want.

I look forward to your reply
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

Moved to the JSP forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

The reason that you got an error was because the tag <c:when> needs to be inside a <c:choose> construct.

Keep it simple. When something doesn't work, find out why rather than trying an overcomplicated alterante way of doing the same thing.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:The reason that you got an error was because the tag <c:when> needs to be inside a <c:choose> construct.

Keep it simple. When something doesn't work, find out why rather than trying an overcomplicated alterante way of doing the same thing.


:jumpingjoy: Oh wow this is awesome now it works. Thanks to you man.

Btw do you have any other simple way to achieve things like this. Showing extra feature for Guest and Member?
I really appreciate your response man! :)

Btw :beerchug:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

I'll usually keep some sort of permissions map for a logged-in user in the session. Then it's easy to use constructs like <c:if> or <c:choose> and friends to make decisions on what to show on the page.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:Moved to the JSP forum.


How do I move this thread to a JSP forum?
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:I'll usually keep some sort of permissions map for a logged-in user in the session. Then it's easy to use constructs like <c:if> or <c:choose> and friends to make decisions on what to show on the page.


Hmmmm permissions map. I never heard of that. Could you elaborate that please. Btw I've make a new Thread topic. permissions map for a logged-in user in the session
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

agustinus lumbantoruan wrote:
Bear Bibeault wrote:Moved to the JSP forum.


How do I move this thread to a JSP forum?


I did it for you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

agustinus lumbantoruan wrote:Could you elaborate that please. Btw I've make a new Thread topic. permissions map for a logged-in user in the session

Please don;t do that. It spreads a discussion over several topics and makes it hard to follow. Let's continue here...
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:
agustinus lumbantoruan wrote:Could you elaborate that please. Btw I've make a new Thread topic. permissions map for a logged-in user in the session

Please don;t do that. It spreads a discussion over several topics and makes it hard to follow. Let's continue here...


I apologize

Yeah so hmm about the permissions map.. is that like the web.xml?
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:
agustinus lumbantoruan wrote:
Bear Bibeault wrote:Moved to the JSP forum.


How do I move this thread to a JSP forum?


I did it for you.


Thanks Bear
Does the topic in JSP page gets the same update as this topic in the Servlet room? So which one should I go to? hahaha confused.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
i am faceing a 404 error.

So it was like this

1. I clicked on the link "Home"
2. Then it leads me to the Indexjsp.jsp page with the header included.
3. But when I clicked on the home link again, it gives me a 404 error.

That is strange.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

If all you need is an indication of whether a user is logged in or not, then it's pretty much just a boolean check. When the user logs in, place an authentication token class in the session. Remove it when the user logs out. If the session expires, it's automatically gone.

The token class can contain whatever info you want, such as the id of the logged-in user, along with other properties that you may want to keep handy.

The presence of the token in the session is an adequate enough test for "logged in". For example, if the session token is given the scoped variable name token:



Simple as that.

If things need to be more fine-grained, such as having a whole slew of permissions that govern what the user can and cannot do, then a Map instance that contains the permissions assigned to the logged-in user can be kept in the token for easy reference.

Even though this article was written some time ago, I think it's still relevant.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

agustinus lumbantoruan wrote:Does the topic in JSP page gets the same update as this topic in the Servlet room? So which one should I go to? hahaha confused.

Doesn't matter. They both map to the same place. The link in the Servlet forum will go away in a few days.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

agustinus lumbantoruan wrote:i am faceing a 404 error.
OK, that's a different issue and one that should be in its own new topic!
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:If all you need is an indication of whether a user is logged in or not, then it's pretty much just a boolean check. When the user logs in, place an authentication token class in the session. Remove it when the user logs out. If the session expires, it's automatically gone.

The token class can contain whatever info you want, such as the id of the logged-in user, along with other properties that you may want to keep handy.

The presence of the token in the session is an adequate enough test for "logged in". For example, if the session token is given the scoped variable name token:



Simple as that.

If things need to be more fine-grained, such as having a whole slew of permissions that govern what the user can and cannot do, then a Map instance that contains the permissions assigned to the logged-in user can be kept in the token for easy reference.

Even though this article was written some time ago, I think it's still relevant.


wow that is something new to me. Servlet Token. I will spent few days starting from now searching and studying Token. Btw, thanks for the elaborating token. I really appreciate it. =)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

"token" isn't anything formal. It's just an object you place into the session to indicate the the user is logged in. It can be complex or simple depending upon your needs.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

P.S. Welcome to the Ranch!
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:P.S. Welcome to the Ranch!


Thanks Bear for writing the article. I shed some light to fix this problem but Im still kinda confused with this "<c:if test="${membership:hasPermission( sessionScope.member, 'ADD_RECIPE' )}">"
-Is that sessionScope a function provided by JSP?
- Does it gives a 500 Error when the GUEST does not have a session object? Because doesn't it gives an error if the users does not have a session
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

You need to wrap your mind around some basic JSP concepts, particularly around the EL. I suggest finding a good modern tutorial.

sessionScope is an implicit variable provided by the JSP engine that is a Map of all scoped variables placed into session scope.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:You need to wrap your mind around some basic JSP concepts, particularly around the EL. I suggest finding a good modern tutorial.

sessionScope is an implicit variable provided by the JSP engine that is a Map of all scoped variables placed into session scope.


Now i've work out how it done

I set

In the LoginServlet this is what I did. I just simplify it
response.sendRedirect(forwardURL);

The above code, set the instance of LogIn as userProfile.
Then forward the response to the forwardURL

After that, I created a simple JSP page.
In this JSP page I would like to add a header but without the Login/Signup form at the top of the header.


The above code, I included the external header.jsp file.

The magic is in the header.jsp


So the most important part in that code is between line 9-19. I call the userProfile.isAMember. isAMember is a getter method which returns a value in boolean.
Then if the return value is equal to 1, it will display a welcome username and a logout button

Else, display the login and signup form

So Mr.Bear, thank you very much for your help. I really appreciate your help and your warm welcome to the forum. God Bless =)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

Glad that you're making progress.

One thing:
. isAMember is a getter method which returns a value in boolean.
Then if the return value is equal to 1

Is it a boolean or an int? It can't be both!

It should be a boolean in which case 1 is not a valid value.

And that makes the expression much better:without the nonsense of comparisons to 1.

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:Glad that you're making progress.

One thing:
. isAMember is a getter method which returns a value in boolean.
Then if the return value is equal to 1

Is it a boolean or an int? It can't be both!

It should be a boolean in which case 1 is not a valid value.

And that makes the expression much better:without the nonsense of comparisons to 1.



Hahahahaha this is funny, sorry it is not a boolean. It's a TINYINT. I chose this so that I can expand it futher instead of 2 (Member and Non Member).
With TINYINT, i could extend beyond 2 possibilities. In the future maybe I could add (Premium member, Moderator, Administrator etc).
That is why i compare ]${userProfile.isAMember} to an integer...Wait hmmm i think it's a String, not an integer. But it works. hahaha
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

Start out with good habits at the outset. Just because "it works" doesn't make it good.

The name of the method is worded such that a boolean is expected. So it should either return a boolean, or the method name should change.

Also using "magic codes" like "1" that need to be hard-coded into the JSP is not a good practice. Rather, I'd have a series of boolean properties that keep things more readable and maintainable. (E.g. isPremiumMember, isNormalMember, isRighteousDude, and so on rather than comparisons against meaningless numbers).
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:Start out with good habits at the outset. Just because "it works" doesn't make it good.

The name of the method is worded such that a boolean is expected. So it should either return a boolean, or the method name should change.

Also using "magic codes" like "1" that need to be hard-coded into the JSP is not a good practice. Rather, I'd have a series of boolean properties that keep things more readable and maintainable. (E.g. isPremiumMember, isNormalMember, isRighteousDude, and so on rather than comparisons against meaningless numbers).


Point taken
If I take that approach all i have to do is using the JSTL



This is cool, Bear. Btw do you write a Java book?">
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

The "eq true" (case counts) is not necessary.

I have proposed a book on proper use of JSP in the post-JSP2.0 world, but no one is interested in publishing it. Oh well.

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Bear Bibeault wrote:The "eq true" (case counts) is not necessary.

I have proposed a book on proper use of JSP in the post-JSP2.0 world, but no one is interested in publishing it. Oh well.



Point taken "eq true" is not necessary

That is a pitty. Maybe it's because there are more PHP and .NET developers compare to JSP/Servlet, im sure there are some people want to read your book. Do you still have a softcopy of your book? =D
Maybe I have a copy please =D. I am so interested to read it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Showing extra header/feature for MEMBER and GUEST