It's not a question of "must", it's a question of whether or not you want to implement a good design. While you could establish the connection in a JSP backing bean class, the generally accepted practice these days is to use the MVC pattern for web apps. DB connections would be part of the "C", so yes - all the DB stuff would be done in a servlet.
I want to do those below:
firstly connected to the database,
and then retrieve the list of categories found in the database,
and display the list of categories in a jsp page in a table.
How can i do it?
thank you in advance
I think you have chosen some very very bad approaches.
Grabbing a connection in a servlet is bad idea.
Grabbing a connection when the servlet initializes and holding onto it until the servlet is destroyed is a monstrously bad idea.
Using Java code in a JSP is a bad idea.
If you are not going to follow the advice given to you, we cannot offer much help.