Sample Application: Login Panel

 

This is an example of a full working JSP application using JSP and Servlet.This program illustrates authentication of a login page, using values from database. An error message will be displayed if the login attempt is a failure. If the login attempt is a success a success message will be displayed and the username is set as a session variable.

This program shows you how to perform authentication using values from database and how to set values with scope session. Session varables are available throughout your application till the scope of session exists.

You can download the source of this Sample Application here.

This is illustrated based on the assumption that you are you're already familiar with JSP, Servlet technologies. and equipped with a Servlet-capable Web server.

Apache Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. You can download the latest version of Apache Tomcat from here

/webapps/

/webapps/LoginPanel/

/webapps/LoginPanel/web/

/webapps/LoginPanel/web/index.jsp

/webapps/LoginPanel/web/success.jsp

/webapps/LoginPanel/web/META-INF/context.xml

/webapps/LoginPanel/web/WEB-INF/web.xml

/webapps/LoginPanel/src/

/webapps/LoginPanel/src/java/

/webapps/LoginPanel/src/java/AuthenticationServlet.java

 

Lets go over each one:

/webapps/

 

The /webapps/ directory is our web server document root.

/webapps/LoginPanel/

 

/LoginPanel/ is the subdirectory where our application is accessed by the browser.

/webapps/LoginPanel/web

 

/web/ is the subdirectory where our JSP files and xml files have been kept.

/webapps/LoginPanel/web/index.jsp

 

index.jsp will be the entry point of our application. The web browser will be accessing this via http://yourdomain.com/Loginpanel. This page contains two fields to enter username and password which is to be authenticated and a submit button to submit the form. This page takes the username and password and pass the values to servlet to perform the authentication.

/webapps/LoginPanel/web/success.jsp

 

success.jsp is the web page which gives you information about the success or failure of the authentication process. It will display a welcome message to the logged in user using the session variable.

/webapps/LoginPanel/web/META-INF/context.xml

 

context.xml will specify the context path to our project. In our case context path is "/loginpanel"

/webapps/LoginPanel/web/WEB-INF/web.xml

 

web.xml acts as the deployment descriptor for our application. It is held in the application's WEB-INF directory. It defines a number of parameters that are used when the web application is deployed into the Tomcat Servlet/JSP container.

/webapps/LoginPanel/src/

 

This folder holds all the source of our web application.

/webapps/LoginPanel/src/java/

 

java This folder holds all the java files necessary for our application. Servlets are also stored in this directory. You can also store the files in a package structure.

/webapps/LoginPanel/src/java/AuthenticationServlet.java

 

AuthenticationServlet.java is the servlet in which we are performing the authentication of the username and password entered by the user. First a database connection is established and tried to select from database a row corresponding to the values entered by the user. No rows selected implies no such user with given authentication present. Otherwise the user is logged in and the username is stored in session.

[Page 1] [Page 2] [Page 3] [Page 4]

 
 
Contact Us