Sample Application: DatabaseAccess
This example executes any query entered by user from the front end. If user is entering a select query result will be displayed as a list. Update and delete queries will be performed and give the success or failure message. The program is implemented using jsp:usebean with database connections and query execution performed in jsp beans.
The <jsp:useBean> element locates or instantiates a JavaBeans component. <jsp:useBean> first attempts to locate an instance of the Bean. If the Bean does not exist, <jsp:useBean> instantiates it from a class or serialized template.
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/DBO/
/webapps/DBO/web/
/webapps/DBO/web/index.jsp
/webapps/DBO/web/dpoperation.jsp
/webapps/DBO/web/META-INF/context.xml
/webapps/DBO/web/WEB-INF/web.xml
/webapps/DBO/src/
/webapps/DBO/src/java/
/webapps/DBO/src/java/com/
/webapps/DBO/src/java/com/dbo/
/webapps/DBO/src/java/com/dbo/DbConnection.java
Lets go over each one:
/webapps/
The /webapps/ directory is our web server document root.
/webapps/DBO/
/DBO/ is the subdirectory where our application is accessed by the browser.
/webapps/DBO/web
/web/ is the subdirectory where our JSP files and xml files have been kept.
/webapps/DBO/web/index.jsp
index.jsp will be the entry point of our application. The web browser will be accessing this via http://yourdomain.com/DBO. This is the index page which is provided with a text area to enter the query to be executed. The program is implemented using usebean with database connections and query execution are performed in jsp beans.
/webapps/DBO/web/dpoperation.jsp
success.jsp is the web page which calls jsp bean to establish database connection and to execute the query entered by the user. The bean will return the result, this page also displays result of the query executed.
/webapps/DBO/web/META-INF/context.xml
context.xml will specify the context path to our project. In our case context path is "/DBO"
/webapps/DBO/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/DBO/src/
This folder holds all the source of our web application.
/webapps/DBO/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/DBO/src/java/com/dbo/
com/dbo Java bean used for executing the query is stored in a package as com.dbo.DbConnection.java. This folder implements the required directory structure.
/webapps/DBO/src/java/com/dbo/DbConnection.java
DbConnection.java is a java file contains three functions. First one is used to establish database connection, second one for executing select queries and the last one is for delete or update queries. Second and third functions calls the first function for establishing database connection.