I want to create an application with three kind of user : administrator, professional and simple user everyone will see special pages, so I must use roles. Do someone have a good example or tutorial on how to do this ? thnx a lot
I would suggest you to go for Spring Security.Have a look at this tutorial
If you don't want to use It you should go for Filter.
Write a filter that will check the USER role manually from DB and according allow or restrict the access
First create a datastore with users and roles and the relationships between them. Most straightforward choice would be a SQL database with user
, role
and user_roles
tables.
Then there are basically two ways to achieve this in JSP/Servlet side.
Homegrow it. Easiest to get started with, but it will end up to be less maintainable in long term. You have to create a HTML/JSP login form, a login servlet to validate, find and login the user and a login filter to check if the user is logged-in and/or has access to the requested resource.
Make use of Java EE provided container managed security. You just have to create a HTML/JSP login form and for remnant it goes all in
web.xml
and server's Realm configuration.
To display specific page content/components based on the user role, you can make use of the flow control tags of JSTL: <c:if>
and <c:choose>
.