5/1/2019
Posted by 
Page

Servlet JDBC Database connection and Log4j integration is the topic of this tutorial. We have provided a lot of tutorials on servlets in java, this tutorial is aimed to use all of those information to create a full-fledged web application with database connectivity and log4j integration for logging.

Use our templates to build a simple Java web app for user login and management with Servlet. Display a customizable user account dashboard after login, only accessible to logged-in users Redirect unauthenticated users who try to access the dashboard to the login page. Step by Step Candidjava beginners guide to develop Jsp servlet login example using eclipse and tomcat.

I strongly recommend to check out following tutorials if you are not familiar with any of these. All of them contains sample project that you can download and run for understanding the core concepts of Servlet API.

Table of Contents

  • 1 Servlet JDBC Example

Servlet JDBC Example

Develop a web application that should have following features.

  1. User can register and then login to the application.
  2. The users information should be maintained in database.
  3. Use standard logging framework log4j.
  4. The application should support session management, no JSPs should be visible without session. Users can logout anytime from the application.
  5. We should not show application and server details to user incase of any exception in application or other common errors like 404.

Once we have our basic application ready, we can move on to add other features.

Design Decisions

  1. Since login page is the entry point of application, we will have a simple login.html where user can enter their credentials; email and password. We can’t rely on javascript validations, so we will do server side validation and incase of missing information we will redirect user to login page with error details.
  2. We will have a register.html from where user can register to our application, we will provide it’s link in the login page for new user. User should provide email, password, name and country details for registration.

    If any information is missing, user will remain on same page with error message. If registration is successful, user will be forwarded to the login page with registration success information and they can use email and password to login.

  3. We will use MySql database for persisting user information. We will create a new database, user and Users table for our application. Since our application totally depends on Database Connection, we will create a servlet context listener to initialize the database connection and set it as context attribute for other servlets.

    We will keep DB configuration details configurable through deployment descriptor. We will also add MySql Java Connector jar to the application libraries.

  4. Since we want to use log4j and configure it properly before usage, we will utilize servlet context listener to configure log4j and keep the log4j configuration XML file location in web.xml init parameters. We will write our application logs in a separate log file dbexample.log for easier debugging.
  5. Incase of any exceptions like “Database Connection Error” or 404 errors, we want to present a useful page to user. We will utilize servlet exception handling and write our own Exception Handler servlet and configure it in deployment descriptor.
  6. Once the user logins successfully, we will create a session for the user and forward them to home.jsp where we will show basic information of the user. We will have a model class User that will store the user data into session. User home page also provide logout button that will invalidate the session and forward them to login page.
  7. We need to make sure all the JSPs and other resources are accessible only when user has a valid session, rather than keeping session validation login in all the resources, we will create a Servlet Filter for session validation and configure it in deployment descriptor.
  8. We will use Servlet 3.0 features for servlet configuration, listeners and filters rather than keeping all of these in deployment descriptor. We will use Eclipse for development and Tomcat 7 for deployment.

Based on above requirements and design decisions, we will create our dynamic web project whose project structure will look like below image.

Let’s understand each one of the components and understand their implementation.

Servlet JDBC Example – Database Setup

We will use below MySql script for new Database, User and Table setup to be used in our application.

Login and Registration HTML Pages

Login and Registration HTML pages are simple pages with form to submit the user information, their code looks like below.

Miele induction cooktop error fe 31. Finally of course it could be internal and would need Miele to investigate and repair. I suspect before you get that far though you will need some evidence the power supply up to their hob isn’t faulty.

login.html code:

register.html code:

Deployment Descriptor and log4j configuration

We will have log4j configuration file inside WEB-INF folder and it will be packaged with the application WAR file.

log4j.xml code:

Our deployment descriptor (web.xml) looks like below.

Notice following points in the web.xml configuration.

  1. login.html is provided welcome file in the welcome files list.
  2. Database connection parameters are made configurable and kept as servlet context init params.
  3. log4j configuration file location is also configurable and relative location is provided as context init param.
  4. Our custom exception handler servlet AppErrorHandler is configured to handle all the exceptions thrown by our application code and 404 errors.
  5. AuthenticationFilter is configured to filter all the incoming requests to the application, this is the place where we will have session validation logic.

Model Classes and Database Connection Manager Class

User.java is a simple java bean that will hold the user information as session attribute.

DBConnectionManager.java is the utility class for MySql database connection and it has a method that returns the connection object. We will use this class for database connection and then set the connection object to servlet context attribute that other servlets can use.

Servlet JDBC Example – Context Listener

AppContextListener.java is the servlet context listener implementation that will initialize the Database connection when application context is initialized and it also configures the log4j using it’s configuration xml file. Notice the use of context init params for DB connection and log4j configuration.

Chuck season 4 download episodes for free

When context will be destroyed, we are closing database connection in contextDestroyed() method.

Since we are using Servlet 3, we don’t need to configure it in web.xml and we just need to annotate it with @WebListener annotation.

Exception and Error Handler

AppErrorHandler.java is our application exception handler servlet configured in deployment descriptor, it provides useful information to the user incase of 404 errors or application level exceptions and provide them hyperlink to go to login page of application.

Servlet Filter

AuthenticationFilter.java is our Filter implementation and we are validating user session here.

Notice the use of @WebFilter annotation, we can also provide URL Patterns for filter here but sometimes it’s good to have in web.xml to easily disable the filters.

Servlet Classes

LoginServlet resource is used to validate the user input for login and forward them to home page or incase of missing data, provide useful information to user.

LogoutServlet is simple and invalidates the user session and forward them to login page.

RegisterServlet is used by users to register to the application and then forward them to login page with registration success message.

Home JSP Page

home.jsp is the home page for user after successful login, we are just showing some user information here and providing them option to logout.

home.jsp code:

The JSP page still contains a lot of java code because we are not using JSP tags, we will look into this in JSP tutorials. As of now please bear with this.

Run the Servlet JDBC Example Application

Our application is ready for execution, I would suggest to export it as WAR file and then deploy to tomcat rather than deploying it directly to Tomcat server from Eclipse so that you can easily look into the log4j log file for debugging.

Some sample execution pages are shown in below images.

User Registration Page:

Registration Success Page:

Login Page:

User Home Page:

404 Error Page:

Input Validation Error Page:

log4j Log File:

dbexample.log:

Download Resources

I hope you liked the article and understand the core concepts of Servlet JDBC and Log4j integration, please share and let me know your opinions through comments. Microsoft toolkit 2 4 8 final han.

Previous
Servlet Exception and Error Handling Example Tutorial

Simple Jsp Servlet Example

1. how to display incorrect pwd on the same login page stackoverflow.com

After checking the database the incorrect password should be displayed on the same login page.I have used servlet and forwarded that to the login page but i couldn't add the message ..

2. How to implement a login page without code duplication stackoverflow.com

I am currently making a dvd browsing system using JSP / Java Servlets and am having trouble figuring out a way to make my login page a bit more efficient.My current ..

3. how to set logo.jpg(image) to login page exclusively stackoverflow.com

i am working on one application which displays the logo.jpg(image) in every page.I want to restrict this image to only login page.Not to the main menu(which navigates after login) and the ..

4. Java EE Login Page Problem stackoverflow.com

i try to code a login form which passes username and password to a servlet and let the user login. Then, in the servlet, i lo but it throws exception ..

5. Facing Problem in making a login page using servlets and jsp stackoverflow.com

i have made a small web application with
form.html
output.jsp
ServletOne.java In the form.html,users enters his name and chooses a icecream flavour
both the username and icecream flavour are set as session attributes
these are ..

6. problem with my login servlet page stackoverflow.com

I'm facing problem with my website where I try to let the user insert the username and password to login to home page, but it show me this error

..

7. Different pages for different login roles stackoverflow.com

I'm a beginner in JSP. While developing a web application for a sample scenario,I came across this subtlety.I have the following pages:

  • index.jsp
  • login.jsp (and LoginServlet.java)
  • account.jsp
To facilitate code reuse I divided all ..

8. how to register data in databse from login page detail in JSP forums.netbeans.org

Hello everyone, i am new comer to web development & netbeans, i m a student. I have been given a task to create registration page with username & password, & after submitting this form it automatically stores data into databse, which can later be seen by admin only..or can be used for retrievel of data for login purpose. I have created ..

9. Login Window in Html page Apache style coderanch.com

This is called Basic Authentication and depends on what HTTP server you are using for how to set it. Some HTTP servers simply have the requirement of placing a special file (eg .htaccess) in a directory you want to protect. The file contains a list of valid users, and when the browser tries to access anything in that directory they are ..


10. Unable to forward page to login.jsp coderanch.com

11. Calling one page to another - login to welcome page coderanch.com

I have a login application and a seperate application which has welcome page. Is there a mechanism utilizes calling one application to another so that after my login is successful, it goes to the welcome page of another application. I know pagecontext probably connects one page to another, but, is there a better option?

13. login page using JSP coderanch.com

15. Blank page on login.jsp (SOMETIMES) coderanch.com


17. Login Page For Jsp coderanch.com

18. login page coderanch.com

hi , i made a login page and i am facing problem in its connectivity with the database (named users). when i enter the email add. and password i should move on to home page but every time the login page displays.. please help me in dis.. the code for login.jsp is: <%@page contentType='text/html' pageEncoding='UTF-8'%><%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %><%@ ..

20. Simple JSP Login Page Help needed coderanch.com

21. Help needed in login page coderanch.com

Hi guys, I have just started learning JSP and implemented login model in my application. Request all of you to kindly go through the code and suggest any recommendation if required. Though there is no error while executing the code and getting the login page . I just wanted to know whether the code is written in proper manner or not. ..

22. Login Page coderanch.com

@Dhivya: No we shall not. As Subhash says, we *will* go to great lengths to help once you get started, or if you have any specific issues, however. Although if you really want to do this entirely in JSP you'll find people less willing to help, since Java code belongs in a Java file, not a JSP! Also, you need to ..

24. Problem in Login page coderanch.com

I have a login page, which directs to Verification.jsp Login.jsp

FreeLancer? Sign up!
Admin
Editor
Journalist
Reporter
Freelancer
USERNAME :
PASSWORD :

26. Login page coderanch.com

I have a login page with username and password. It goes to a servlet verifies credentials and if the user enters wrong password for 3 times then the account is locked for 15 minutes. If the login is success then sets up the data in session and redirects to the home page. Is this enough for security purpose ? And as ..

27. accessing my login.jsp page as http://localhost:8081/Login forums.oracle.com

28. LDAP server login in JSP page forums.oracle.com