Login Example: JSP and Servlets: Part II.

Hello Friends,

This is part II of jsp and servlets login example. Check Part I here.

Step 4) Now, we need to create a Servlet file for the redirection of the jsp pages. In the src folder under the package code2java, add the Servlet file and name it as LoginServlet. As discussed earlier this servlet file will get the user details from the Bean file and then it will create a Session for that user only if the user is a valid user. And accordingly it will redirect to the respective jsp pages. You can use the default code which Eclipse generates while Servlet creation, the only change you need to do is in doGet() method. The code for this is written below; add this code in LoginServlet.java file.

Step 5) You can see in the above code we have created the object of the Bean class LoginBean.java so that we can set the user inputs to a specific value and then use these values for further action which will be performed by DAO. So, we will create a LoginBean class first and then the DAO.

Step 6) In the src folder, under the package code2java, create a java class, name it as LoginBean and add the below code. This contains the setters and getters for setting up the values.

Step 7) Now create another class under code2java package, name it as LoginDAO and add the below code. This DAO gets the requested values from the database using the DB query, in this case sql query. This query is created as per the inputs provided by the user on the LoginPage which is a JSP file. And as per the result of the executed query it will return the boolean value to the LoginBean.

We have implemented the DAO to access the data from DB, but we have not yet created the required database. This we will see in the next tutorial.

<< Previous    |      Next>>

Thanks,

Nikhil Naoghare.

2 thoughts on “Login Example: JSP and Servlets: Part II.”

  1. Hey, Nikhil, nice job! Thanks for the effort. This is one of the most straightforward tutorials I’ve found.

    One small – okay, not so small – point: you posted LoginServlet.java code twice, once where it belonged and then again for LoginDAO. Could you please add the LoginDAO.java code when you get a chance.

    Thanks.

    1. Hello Greg,
      Thanks for checking out that mistake 🙂 I have changed the existing code for LoginDAO and added the code which is required and correct. Please have a check now.

      Regards,
      Nikhil.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.