Suggested articles for you:

21 thoughts on “Session and Role based Java Login example

  1. Ferry

    Ravi I tried using your code but it always give me an error : Error message = Invalid user credentials .Although I am using the correct credentials for the login process.

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Ferry,

      This is strange.
      Did you verify the user details in your database? Make sure your role is also matching along with the user details.

      One of the following conditions must be satisfied in order to log-in successfully.
      if(userName.equals(userNameDB) && password.equals(passwordDB) && roleDB.equals("Admin"))
      return "Admin_Role";
      else if(userName.equals(userNameDB) && password.equals(passwordDB) && roleDB.equals("Editor"))
      return "Editor_Role";
      else if(userName.equals(userNameDB) && password.equals(passwordDB) && roleDB.equals("User"))
      return "User_Role";

      You can also go through the video https://youtu.be/swdx5g0X1hk to understand the flow.

      Reply
  2. Martijn

    Hi Ravi – I followed your tutorial on youtube “Session and Role based Java Login example”. Very helpful and everything is working fine. But for my school exercise I want to change the 3 roles and add 3 additional roles. But as soon as I change e.g. Admin to a different name (in all code and classes) it keeps on saying invalid user. Any tips on how I can change the roles and add additional ones? Thanks in advance!

    Reply
    1. rahul

      connected with database but olways show err message with user name. how is this possiible

      Reply
      1. rahul

        Error message = abhi
        abhi
        Post establishing a DB connection – com.mysql.jdbc.JDBC4Connection@6541f4
        Error – abhi
        Error message = abhi

        this type of error occur everytime….
        help me

        Reply
  3. Shanmukh Madhavarapu

    I have followed every step of it, but I am getting following error.

    The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

    Path in URL is:http://localhost:8080/Sample/LoginServlet

    Can you tell me what is the problem?

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Shanmukh,

      Looks like you are maintaining different directory structure. Did you modify the web.xml and action=”” attribute in the Login.jsp accordingly?

      Also, look for the welcome-file in the web.xml.

      If you use my code as it is, you should be able to access login form with the URL – http://localhost:8080/LoginRoleSession/

      You can also refer to the video for a clear understanding – https://www.youtube.com/watch?v=swdx5g0X1hk

      Let me know if this does not solve your problem.

      Reply
        1. Ravi Bandakkanavar Post author

          I want to know if your LoginServlet is being called or is it failing while routing requests to LoginServlet.

          Please add sysout statements in LoginServlet. You can try displaying username input in the beginning of the servlet.

          Have you kept web.xml under WEB-INF folder?
          Are you using request.getContextPath()? Try removing it.

          What tomcat version are you using?

        2. Shanmukh Madhavarapu

          I am getting error in initiating the servlet. I am uisng tomcat9 server

        3. Ravi Bandakkanavar Post author

          Yes. It is not able to locate your servlet. In the action just specify LoginServlet as shown in the link http://krazytech.com/programs/a-login-application-in-java-using-model-view-controllermvc-design-pattern

          1.Hope you are using doPost method and your code look like following
          public class LoginServlet extends HttpServlet {

          public LoginServlet() {
          }

          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          2. Also, try loading servlet-api.jar in the execution path.

          3.you can simply start with a new project by copying code for login and servlet only two files.

Did it help? Comment here..