Search code examples
javajspservletsintranet

Roles in JSP and Servlet


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


Solution

  • 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.

    1. 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.

    2. 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>.