Learn Servlet-JSP By Building A Social Network Website — Part II

Sera Ng.
Tech Training Space
5 min readOct 22, 2020

In this part, we are going to set up our database and explore the servlet model.

Task 2: Setup Database

You can download the script file (cbooksocial.sql) to create the database. The script file includes database creation as well as sample data.

https://github.com/stackera/learn-servlet-jsp/blob/main/db.zip

I have also added the diagram file (cbooksocial.mwb) in case you want to have a visual view of the database. You can open this file in MySQL Workbench

tbl_user

tbl_friend

This table stores information about friends of a user

tbl_post

tbl_comment

Task 3: Reading — Servlet Model

First, let’s take a look at an overview of how the web application model works:

When you type a web address in your browser and hit enter, basically the following processes are happening:

1:

The web browser sends an HTTP request to the web server where your web application is running.

2:

The web application receives the incoming request from the browser. It checks to see what the web browser wants and performs business logic as required. There are a lot of languages and technologies that can be used at this stage such as Java, C# .NET, PHP, Python, and so on.

3:

Usually, business logic needs data from the database. Therefore, the web application needs to connect to the database system, perform SQL statements, and returns the results.

4:

The web application then needs to select appropriate content to send back to the web browser. The content may be in a variety of formats such as HTML, a file, and JSON.

5:

The web server sends back the established response to the web browser. The web browser renders the received results to the user.

And here is the web application model with Java Servlet involved

As you can see, Servlet can be used to perform the following tasks:

  • Receive requests from the web browser
  • Process business logic
  • When required, connecting to a database system to retrieve data
  • Create responded content and send back to the web browser

If we delve into a servlet, there are 3 steps involved in its life cycle

1: When a servlet receives the first request, the init() method is invoked. Usually, we might place the initialized code in this context. Keep in mind that this method will only be called once during the servlet’s life cycle. Originally, it is an abstract method. Therefore, it is optional to be overridden in your servlet.

From the second request onwards, they will go straight to the service() method to be processed

2: Once the init() method completes its execution (if being overridden), then the service() method is invoked to actually process the browser’s requests. This is where all related business logic happens.

It is worth mentioning that from the second request onwards, they will go straight to the service() method to be processed.

Actually, the service() method is seldom directly used to handle requests. Rather, either doGet() or doPost() will handle requests depending on the corresponding get or post method.

When a browser sends some data to a servlet, the browser usually needs to form data in a sending method. There are many methods that can be utilized but the most popular ones are Get and Post.

The following table summarizes some important points between Get and Post

Read more HTTP methods at https://en.wikipedia.org/wiki/POST_(HTTP)

3: When the servlet is destroyed, the destroy() method is invoked. We usually place some code that cleans up resources in this content.

Similar to the init() method, the destroy() method is optionally overridden to be executed.

As just mentioned, HTML content can be generated by a servlet. However, in reality, we never want to do that. That is because fundamentally, a servlet is just a normal Java class. Therefore, in order to generate HTML content from a servlet, we need to embed HTML tags in Java code in string format.

Here is an example of a servlet with HTML content:

Surely enough, we don’t want to get into that mess-up.

That comes to a reason we need another way to easily create HTML content and also, be able to receive data from a servlet.

The solution is JSP (Java Server Page) technology.

The main purpose of JSP is to provide developers a convenient way to work with front-end code while can also handle back-end code such as receiving data from a servlet and other normal Java code.

However, JSP is not a separate technology. A JSP fundamentally is a servlet. It just provides a different approach for those who work with front-end code more easily.

Therefore, when a JSP page is requested, the first step is that the servlet engine, which is Tomcat, will perform the translation process, in which the whole JSP file will be converted to a servlet format, and then goes through all the stages as in servlet’s life cycle that we have just described above.

Here is a sample of a JSP file:

We just need to add a directive at the beginning:

<%@page contentType=”text/html” pageEncoding=”UTF-8"%>

to indicate that this is a JSP file so that it will be translated into a servlet before serving requests from the browser.

One last thing we need to mention about JSP is that although most of the code in a JSP file is HTML tags, we can actually embed normal Java code in a JSP file where we see fit. However, we should do that with care since messy code (HTML tags and Java code) might be all over the place.

You will learn more about Servlet and JSP along the way of building this project.

--

--