?
Solved

JSP User login

Posted on 2003-03-04
15
Medium Priority
?
453 Views
Last Modified: 2010-04-01
Hi all. I come from PHP where this matter was very easy. Now i am working on the web application, where clients are html files with applets, dont ask why. So web app wont let user access any html page without logging before. This can't be done in html, but i think i could use jsp instead of html. In PHP it was easy - just add line include("login.php") on the top of every page. Another problem is that i need to check if user exists in db via servlet. So, user is accessing jsp page, jsp needs to check if user is logged, if not print html form, get the login data and send login info to the servlet (how - by FORM ACTION or...). Servlet puts the user in the session, and REDIRECTS user to the jsp page where he wanted to enter. How could i do this without much pain. I think 20 points is enough for people who have done this before.

Radovan
0
Comment
Question by:rrasho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 4

Expert Comment

by:thanassis
ID: 8064152
1. with jsp you can also include a file on the top of every page:
<%@ include file="login.jsp"%>

2. you have to write java classes for db connection and user validation.

3. use these classes in jsp pages:

<%   if (MyValidationClass.getIsLoggedIn()) { %>
....
...
...
<%   }      %>
0
 

Author Comment

by:rrasho
ID: 8064195
Sorry, i am still ignorant in these things but i think include works like this:
http://path/Page.jsp, and Page.jsp has got in the first line login.jsp, but it will just display login.jsp page and Page.jsp as well, while PHP is stopping Page.php and displays login.php until user is logged in.
And like i said, i want to do user validation in Servlet.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8064790
use:
<%@ include file="checklogin.jsp"%>

in checklogin.jsp (psudo code)

if (user is not login in) {
  response.sendRedirect( "/login.jsp" );
  return;
}

in login.jsp, your form submit user name and password to your Servlet.
0
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!

 
LVL 19

Expert Comment

by:cheekycj
ID: 8065331
using a filter maybe a better approach than including a jsp in every page.

CJ
0
 

Author Comment

by:rrasho
ID: 8070173
I think filters are too difficult to me because my knowledge about java and web apps is very poor. All i do is translating code from other languages i used to know :(

I like comment from kennet, and i think it could help.
Now, if user and pass dont match, how can i redirect again from servlet to login, and if it is ok, how can i redirect to the page from where include("checklogin.jsp") is called?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8073006
filters are very easy.  They may seem difficult but if you read 1 or 2 articles on them you will realize that they are very simple.

Plus every time you create a jsp or modify something you don't have to worry about the include file.

Check out:

http://java.sun.com/products/servlet/Filters.html
http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters.html

a combination of your web.xml and a filter for each level will make this much easier.  Then you don't need to modify any of your JSPs.. the authentication is handled separately.

CJ
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 60 total points
ID: 8073568
>> Now, if user and pass dont match, how can i redirect again from servlet to login

 response.sendRedirect( "/login.jsp" );
 return;

>> and if it is ok, how can i redirect to the page from where include("checklogin.jsp") is called?
that's a little tricky:

in checklogin.jsp, you save current location in session:
if (user is not login in) {
 session.setAttribute( "fromURL", request.getRequestURL() );
 response.sendRedirect( "/login.jsp" );
 return;
}

in servlet:
 response.sendRedirect( request.getSession().getAttribute( "fromURL" ) );
 return;
0
 

Author Comment

by:rrasho
ID: 8078223
Hello guys.
Thanks for the help. I gave up jsp and i am doing all stuff with servlets, one servlet will receive all requests and then if not logged will give login form and then redirect to URL that is requested, and this is remembered in the session.
I give my points to kennet
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8085377
B grade :(
0
 

Author Comment

by:rrasho
ID: 8088402
Sorry kenneth, i dont get what you mean by B grade?
Does it mean you didnt get enough points, or is this some kind of a joke?

Radovan
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8089995
right, you always spent 20 points regardless of grade given. but we get less points with 'B' grade compare to 'A' grade :)
0
 

Author Comment

by:rrasho
ID: 8090082
Yes, the answer was ok, but i had to figure out some more things, so i graded it with like B+. Sorry and tks again
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8090151
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8090153
I can get it fixed, is it ok for you?
0
 

Author Comment

by:rrasho
ID: 8090215
If it is important i can give you an "A" and if that is possible, ok?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

800 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