Create JSP CRUD Application and run on Tomcat 7

In this section we will discuss about how to create a simple crud application
in JSP using Eclipse IDE. We will use Eclipse to compile and deploy the
application on Tomcat 7 server.

CRUD is basically used in the context of database driven application where it Creates,
Reads, Updates and Deletes the data of database. CRUD is also know as create,
read, update and delete. The example given here will teach you how easily you
can create application that insert, query, update and delete the data. Create means to insert
a record into the database table, Update means edit/make changes into the
existing record, and the Delete means deleting of record from the table. We can
also create a CRUD application using JSP, Servlet, And the JDBC.

Here is the video tutorial of: "JSP CRUD application example"

Example

Here we will create a CRUD application using Java Servlet, JSP, and the JDBC.
We will understand here the basic purpose of creating a CRUD application by a
registration form where the user will registered by providing the id, first
name, and last name. Update/Edit their records, and can delete their record
also. For this application we will required some front end interfaces for the
registering a user and updating the record. For creating the interfaces I have
created the JSP pages such as user.jsp page for adding new user this page
contains a form and the respective input text fields for providing their
information and the submit button as well as created edit.jsp page where the
selected record's ID will be showed in the readonly textfield and the other
textfields for updating the respective fields. Then I have created a Java Beans
named UserBean.java which contains some data members (id, fName, lName) and
their setter getter methods. These setter getter methods are used for setting
and getting the corresponding value respectively. Then I have created a
ConnectionProvider class to connect with the existing database (in my case I am
using MySQL). Then I have created a DAO class where I have written the code for
adding the user, editing the user record, deleting the user record, getting the
all user record, and for getting user record by ID. Then I have created a
Servlet where written the code to handle insert, edit, and delete records.
Finally I have created a listUser.jsp page for displaying all records and the
link for updating and deleting the records. In this application you can insert,
edit, or delete application one by one.

When you will execute the above example you will get the output as follows :

1. Database table before inserting the record

2. After executing this example the user.jsp page will be opened from here
you can insert the new record and view the record's available in the table.

3. After providing the fields value ( I have inserted two records) when you
will click on the submit button, in the above image, the page will be forwarded
to the listUser.jsp as follows :

In the above image you can see a hyperlink "Add User", this is for adding
more new user as well as there are two more hyperlinks "Update" and "Delete"
update is used for editing the existing record (discussed later) and the delete
is used for deleting the existing record (discussed later).

And when you will see the database table you will see these two records are
also added into the table as follows :

4. Updating record : When you will click on the hyperlink Update shown at the
listUser.jsp page an interface for updating the corresponding record, such as I
want to edit the record with user ID 1, will be opened as follows :

5. Here You can alter the record of user ID 1, such as I have changed the
Last Name from Singh to Roy and keep the Deepak as it is then the output will be
as follows :

And the database table after update will be look like as follows :

6. Now I want to delete the record with of User ID = 2 So, I have click on
the hyperlink delete in the second row whose id is 2 the record is deleted and
then after the remaining list is as follows :

Fraise
March 10, 2013
re
Thanks, it is very helpful even if you have used the get method instead of the post method in the servlet class which is not very secure. But it is clear and simple.

pallavi
September 13, 2013
list not displaying
when i am adding the user i am not able to see the displayed result by using the same code.
i am using oracle and all credentials have been set.
there are no errors but once i add the msg of added successfully is displaying but not the list.

KAVI
October 24, 2013
pls provide the solutoion
after running the user.jsp i got this error ....pls provide the solution
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
ConnectionProcess.UserDao.addUser(UserDao.java:29)
HandlerProcess.UserHandler.doGet(UserHandler.java:41)
HandlerProcess.UserHandler.doPost(UserHandler.java:87)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.33 logs.

Mangesh Mandavgane
January 7, 2014
edit page not display
I have done Select,delete and add operation successfully but when trying to doing edit(update) it will not open the edit.jsp...

rakesh
January 29, 2014
Exception in Code
Hi,
i am Rakesh ,I was downloaded code(Curd jsp servlet jdbc) from ur tutorial , While running this code inside server I got Exception like..
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Jan 29, 2014 10:58:42 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at info.dao.UserDao.getAllUsers(UserDao.java:71)
at org.apache.jsp.listUser_jsp._jspService(listUser_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
pls replay how i can resolve this exception
Thanks & regards
Rakesh

the employe
March 5, 2014
ask
Hi, when I try to run my project it seems ok, but when i clik button submit the page directly go to here :
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
net.roseindia.dao.UserDao.getAllUsers(UserDao.java:75)
net.roseindia.handler.UserHandler.doGet(UserHandler.java:76)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
please help me..
thanks

trank
March 10, 2014
ask
good tutorial,
may i know, how to set combo box to save in database?
because in your tutorial at user.jsp, just using input type = "text".
can you give me some example for combo box?
Thnks

Sanketh
March 11, 2014
error when submit is pressed
Error 404: javax.servlet.UnavailableException: SRVE0200E: Servlet [net.roseindia.handler.UserHandler]: Could not find required class - net/roseindia/handler/UserHandler
i am getting this error after step 2 step that is when i press submit after filling the form.