473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

checking if user is login or not

59
I have a index page of my website. it will change if user is login or not. I know how to do just by using jsp. but this is not the right way bc I am using java code in html file.

so here is ex of doing it just by jsp
index.jsp
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.     <%
  4.         if (session.getAttribute("username") != null) {
  5.             out.print("
  6.                             <div>
  7.                                      user | option | logout
  8.                               </div>      
  9.                             ");
  10.         } else {
  11.             out.print("
  12.                          <div>
  13.                               login | forgotpassword | create account
  14.                           </div>");
  15.         }
  16.     %>
  17. </body>
  18. </html>

so I want to do the same thing only doing it by the right way(jsp/servlets). I tried look up but cant find any thing.
Dec 17 '13 #1
4 7172
chaarmann
785 Expert 512MB
Where is the problem?
You know how to do the login page as a a servlet (see your immediate previous post).
When the user goes through the login page successfully, you just put an attribute into his session, like isLoggedIn=true.
On every other page, you will check the session for this attribute, and if true, continue on the current page, else redirect to the login page.

Instead of putting in a isLoggedIn boolean, I used a user-object. If the user-object was not in the session, then I redirected to the login page. This check was done in a base servlet where all other servlets and JSPs were extended from.
Dec 18 '13 #2
game2d
59
i c. but wont i run into same problem? ex:

Expand|Select|Wrap|Line Numbers
  1. <%
  2.         if (session.getAttribute("username") != null) {
  3.            isLoggedIn = true;
  4.         }
  5.         else{
  6.            isLoggedIn = false;
  7.         }
  8.  
  9.  
  10.  
  11.        if(isLoggedIn){
  12.         ....
  13.        }
  14.        else{
  15.        ....
  16.        }
  17. %>
Dec 18 '13 #3
chaarmann
785 Expert 512MB
Above code only has a local variable isLoggedIn.
That's not what I mean.
You will put the isLoggedIn in your session (with session.setAttribute(). You will do that in your result page of your login-page when you have verified the password successfully.

Second, you will not test for isLoggedIn in JSP. As I mentioned above, you will do these checks in a base-servlet.
There you have the checking code only once. When you derive other pages from this baseServlet (MyActionSevlet extends BaseServlet), the checking will be done for all other pages automatically.
The check will be done as the first thing when the client request arrives. You progam the check in that way: if session.getAttribute("isLoggedIn") == true, you continue with the action in your derived class, else you redirect to a login page. This action will do all the business stuff and housekeeping (model), and to create a resulting HTML page for the client you call a JSP page (view) at the end.

I must empahsize on this: just use MVC strictly. (Model-View-Controller). Do not do any business logic (like DB-access) in your JSP. Do it in the servlet. On the other side, do not do any GUI creation (like HTML tables) in your servlet, do it in your JSP.
Dec 19 '13 #4
game2d
59
if its not too much to ask can you give a example? Just so I can understand it better.
Dec 23 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bennett Haselton | last post by:
I'm looking for a PHP tutorial that specializes in how to build sites that are based around user logins. i.e. the user logs in on the front page, and are taken to a main login page where fields on...
3
by: Joey Lee | last post by:
Hi, Does anyone knows how to control user login that only a single userid can login at a time? Thanks Joey
15
by: bb nicole | last post by:
The company profile showed blank after user login and click the company profile button. Supposed the company profile should not be blank and will show the company information which call from...
2
by: antonyliu2002 | last post by:
I am testing ASP.NET 2.0 Forms athentication with user credentials in SQL Server 2005. I don't want to put user credentials in web.config, so the credentials section is commented out. The...
2
by: momogi | last post by:
Hi theScripts! I have a problem with my session. When user login in my site, the session will save the user name and password. In my Login.php I have: session_start(); $qr="SELECT*FROM...
22
by: klenwell | last post by:
I'm in the process of refactoring the php code base I've amassed over the last few years into an object-oriented framework. I'm about to start in on the authentication/login extension and I've...
12
by: tvnaidu | last post by:
I have Two kinds of web pagess, one is for control page for only admin login, another one is to view status for user login. initially both html files can view with 192.168.0.10/control.htm and other...
4
by: sightless | last post by:
Hi all, Is it possible to capture Windows user login in Unix? We have a AIX Unix system that allows users to login (using a common login) and run scripts. At the end of the day, we cannot find out...
5
by: anewuser | last post by:
I currently have a working multi user login form in access 2007. It automatically redirects people who login to a user specific form e.g. user = staff1 is moved to form staff1 and manager1 is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.