Creating Data Centric Applications with JSP Servlet

Servlets and Java Server Pages (JSP) are the highest level views of network programming in Java. Together they form the foundation of the request response model of communication. A servlet basically is a Java class that extends the functionality of a server, such as a Web Server that serves web pages to a user's browser using the HTTP protocol. JSP allows us to create pages that encapsulate Java functionality and even to write scriplets of actual Java code directly into the page. These features along with JDBC can be used effectively to create a data centric network application very easily.

Basic Requirements

The basic requirements of creating such an application are as follows:

Note: NetBeans version 8 IDE and its predecessor has inbuilt all the requirements as described above, except a database package such as MySQL. To start with this will be the best IDE, I believe. In this article we will be using Netbeans IDE, Tomcat Application Server and MySQL Database at the back end.

NetBeans IDE 8.0 Download

Web Programming Fundamentals

When implementing the request response model of programming, it occurs between web browsers and web servers. When a user selects a web site to browse through the browser (the client application), a request is sent to the appropriate web server (the server application). The server normally responds to the client by sending the appropriate HTML web page. Let us view this from the perspective of a Tomcat Server.

Request Response Model

Tomcat refers to a servlet by the name given in the urlPatterns (if annotation is used rather than web.xml) and not by the servlet file name. The parameter urlPatterns is defined in the @WebServlet annotation as follows.

@WebServlet(name = "AddNewServlet", urlPatterns = {"/AddNewServlet"})

To write a servlet, we need to extend the HttpServlet class then use the annotation @WebServlet (See listing 3). The parameter, urlPatterns, is the key that tells Tomcat to execute the relevant classes whenever the requested string pattern matches with this urlPatterns. Observe that whenever we enter, http://localhost:8080, in the browser a Tomcat instance runs, as we add our project name such as http://localhost:8080/JDBCMVCApp, the first file mentioned in the web.xml runs. Let us create a small but complete data centric web application illustrating the use of servlet, JSP, JDBC using the Model View Controller (MVC) model of programming.

Our Application Design

The MVC model of programming grossly consists of four layers – Controller, Business Service, Model and View. Let's understand them from our project point of view.

Controller

AddNewServlet, DeleteServlet, UpdateServlet

Business Service

EmployeeBean

Model

Employee

View

empAddNew.jsp, empUpdate.jsp, empView.jsp

Controller, is a servlet in our case. When a request to add a new employee is initiated and the values for new employee are passed on to the Tomcat server, our controller servlet, AddNewServlet intercepts this request and collects the request parameter values for further processing. However, Controller servlet does not actually accesses the database. This is done through the Business Service class.

Business Service is a simple Java bean containing business logic and performs the actual processing function in the application. In our case, EmployeeBean contains all the CRUD logic for database access and provides service when called from the controller servlet.

Model contains the information and acts as an abstraction to the actual database schema. Obviously, this whole process needs some interface where the client can interact with the application – for example, forms for data collection, viewing, modifying, etc. We have used JSP to create all the view layer files, because, in JSP, along with HTML, we can write Java code seamlessly. This has a double advantage – one, the browser can easily render HTML and two, the programmer can write java code within HTML. As a result WYSIWYG HTML comes alive with dynamic content written in Java.

Then we write Business Service class: EmployeeBean.java. This class is the heart of our application. Observe the boilerplate code for database connection is repeated to retain simplicity. Once mastered the “way of the web programming in Java” :) the code can be further tuned to make it concise.

Now, the view JSP files: empView.jsp, empAddNew.jsp, empUpdate.jsp. These JSP files also include two additional files – one JSP file named header.jsp, which includes some demo header information and links and another, a cascading style sheet named style.css to configure HTML rendering in the browser.

Configuring NetBeans

Select Java Web from the Categories list and Web Application from the Projects list. Click Next.

Provide the name of the Project and click Next.

Choose Apache Tomcat Server from the Server drop down list and click Next.

Finish.

Now, do not forget to include JDBC driver in the Project → Libraries. JDBC driver is not included by default in the project. As we have used MySQL JDBC Driver, to include it right click on the Libraries → choose Add Library → click on import... then select the appropriate JDBC driver from the list of available libraries and → click Import Library.

Creating a Sample Database for the Project

Create a sample table in the MySQL database for testing and realizing the above project. Open a MySQL prompt and type the following query:

Conclusion

Integrating JDBC, JSP and Servlet is pretty straight forward. This type of project is mainly used for creating multi-tiered online applications using request response mechanism. JSP technology is basically an extension of servlet technology and simplifies the process of creating pages by separating presentation form the content. JSP, Servlet and JDBC are a popular combination for creating dynamic web pages and applications with extensive data processing.

IT Solutions Builder
TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD

Which topic are you interested in?

Mobile

Security

Networks/IoT

Cloud

Data Storage

Applications

Development

IT Management

Other

What is your company size?

What is your job title?

What is your job function?

Searching our resource database to find your matches...

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date