Suggested articles for you:

10 thoughts on “Session and Role based Java Login example

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