[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 532
  • Last Modified:

JSP User login

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
rrasho
Asked:
rrasho
  • 6
  • 6
  • 2
  • +1
1 Solution
 
thanassisCommented:
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
 
rrashoAuthor Commented:
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
 
kennethxuCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cheekycjCommented:
using a filter maybe a better approach than including a jsp in every page.

CJ
0
 
rrashoAuthor Commented:
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
 
cheekycjCommented:
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
 
kennethxuCommented:
>> 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
 
rrashoAuthor Commented:
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
 
kennethxuCommented:
B grade :(
0
 
rrashoAuthor Commented:
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
 
kennethxuCommented:
right, you always spent 20 points regardless of grade given. but we get less points with 'B' grade compare to 'A' grade :)
0
 
rrashoAuthor Commented:
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
 
kennethxuCommented:
I can get it fixed, is it ok for you?
0
 
rrashoAuthor Commented:
If it is important i can give you an "A" and if that is possible, ok?
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now