Saturday, January 23, 2021

Setting up Connection pooling via JNDI in TomCat 9.0.41 Java Web Server

This Howto follows official guidelines and might be a bit more straight forward and easier to reproduce then original . That is a way I was able to get JNDI up and running on 9.0.41 release . I have also compiled in one post displaying mariadb's table data via Servlet and via JSP based on utilizing tags supported via the most recent versions of taglibs 1.2.5.

See for details  https://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html  

 Overall application file layout .















[tomcat@sever33fedora ConnectionPool]$ ls -CRl

.:

total 8

drwxr-x---. 2 tomcat tomcat  24 Jan 23 20:33 images

-rw-r-----. 1 tomcat tomcat 752 Jan 23 16:22 index.html

drwxr-x---. 3 tomcat tomcat  56 Jan 23 20:33 META-INF

-rw-r-----. 1 tomcat tomcat 558 Jan 23 18:33 showbooks.jsp

drwxr-x---. 5 tomcat tomcat  58 Jan 23 20:33 WEB-INF

./images:

total 4

-rw-r-----. 1 tomcat tomcat 1441 Jan 22 22:02 tomcat.gif

./META-INF:

total 4

-rw-r-----. 1 tomcat tomcat 63 Jan 23 20:24 MANIFEST.MF

-rw-r-----. 1 tomcat tomcat  0 Jan 23 20:33 war-tracker

./WEB-INF:

total 4

drwxr-x---. 3 tomcat tomcat  19 Jan 23 20:33 classes

drwxr-x---. 2 tomcat tomcat 237 Jan 23 20:33 lib

drwxr-x---. 3 tomcat tomcat  47 Jan 23 20:33 src

-rw-r-----. 1 tomcat tomcat 800 Jan 22 18:59 web.xml

./WEB-INF/classes:

total 0

drwxr-x---. 2 tomcat tomcat 36 Jan 23 20:33 mypkg

./WEB-INF/classes/mypkg:

total 4

-rw-r-----. 1 tomcat tomcat 3381 Jan 23 18:32 MySQLDbcpServlet.class

*******************************************************

All required jars placed under WEB-INF/lib    from https://tomcat.apache.org/download-taglibs.cgi

Mysql-connector-java-8.0.23.jar is also present in $CATALINA_HOME/lib and includes in $CLASSPATH of "javac" build of MySQLDbcpServlet.java

*******************************************************

./WEB-INF/lib:

total 4440

-rw-r-----. 1 tomcat tomcat 2415211 Jan 21 18:17 mysql-connector-java-8.0.23.jar

-rw-r-----. 1 tomcat tomcat 1660960 Jan 21 18:17 protobuf-java-3.11.4.jar

-rw-r-----. 1 tomcat tomcat   52794 Jan 23 15:54 taglibs-standard-compat-1.2.5.jar

-rw-r-----. 1 tomcat tomcat  206430 Jan 23 15:54 taglibs-standard-impl-1.2.5.jar

-rw-r-----. 1 tomcat tomcat  163271 Jan 23 15:54 taglibs-standard-jstlel-1.2.5.jar

-rw-r-----. 1 tomcat tomcat   40153 Jan 23 15:54 taglibs-standard-spec-1.2.5.jar

./WEB-INF/src:

total 4

-rw-r-----. 1 tomcat tomcat 2714 Jan 23 18:32 MySQLDbcpServlet.java

*********************************

web.xml of particular application

********************************

[tomcat@sever33fedora ConnectionPool]$ cd WEB*

[tomcat@sever33fedora WEB-INF]$ cat web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

    version="2.4">

    <resource-ref>

      <description>DB Connection Pool</description>

      <res-ref-name>jdbc/TestDB</res-ref-name>

      <res-type>javax.sql.DataSource</res-type>

      <res-auth>Container</res-auth>

      <res-sharing-scope>Shareable</res-sharing-scope>

   </resource-ref>

   <servlet>

      <servlet-name>MySQLDbcpExample</servlet-name>

      <servlet-class>mypkg.MySQLDbcpServlet</servlet-class>

   </servlet>

   <servlet-mapping>

      <servlet-name>MySQLDbcpExample</servlet-name>

      <url-pattern>/trydbcp</url-pattern>

   </servlet-mapping>

</web-app>

***************************************************

$CATALINA_HOME/conf/context.xml contains Recource

entry available over all apps on Tomcat 9.0.41

***************************************************

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

     maxActive="100" maxIdle="30" maxWait="10000" removeAbandoned="true"

     username="root" password="root" driverClassName="com.mysql.jdbc.Driver"

     url="jdbc:mysql://localhost:3306/ebookshop" />

****************

Servlet code

***************

[tomcat@sever33fedora src]$ cat MySQLDbcpServlet.java

package mypkg;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

import javax.sql.DataSource;

import javax.naming.*;

public class MySQLDbcpServlet extends HttpServlet {

   DataSource pool;  // Database connection pool

   @Override

   public void init( ) throws ServletException {

      try {

         // Create a JNDI Initial context to be able to lookup the DataSource

         InitialContext ctx = new InitialContext();

         // Lookup the DataSource, which will be backed by a pool

         //   that the application server provides.

         pool = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");

         if (pool == null)

            throw new ServletException("Unknown DataSource 'jdbc/TestDB'");

      } catch (NamingException ex) {

         ex.printStackTrace();

      }

   }

   @Override

   public void doGet(HttpServletRequest request, HttpServletResponse response)

               throws IOException, ServletException {

      // Set the response message's MIME type

      response.setContentType("text/html;charset=UTF-8");

      // Allocate a output writer to write the response message into the network socket

      PrintWriter out = response.getWriter();

      Connection conn = null;

      Statement  stmt = null;

      try {

         out.println("<!DOCTYPE html>");

         out.println("<html>");

         out.println("<head><title>Qurey Servlet</title></head>");

         out.println("<body bgcolor='C0C0C0'>");

         out.println("<h3>Servlet displayes table content via connection pool</h3>");

         out.println("<br>"); 

         // Get a connection from the pool

         conn = pool.getConnection();

        // Normal JBDC programming hereafter. Close the Connection to return it to the pool

         stmt = conn.createStatement();

         ResultSet rset = stmt.executeQuery("SELECT title, author ,price, qty  FROM books");

         int count=0;

         out.println("<table border='2'>");

    while(rset.next()) {

       out.println("<tr><td>" + rset.getString("title") + ", "

      + rset.getString("author") + ", " + String.valueOf(rset.getDouble("price")) + ", " + Integer.toString(rset.getInt("qty")) +  "</td></tr>");

          ++count;

        }

         out.println("</table>");

         out.println("<p>==== " + count + " rows found =====</p>");

         out.println("</body></html>");

      } catch (SQLException ex) {

         ex.printStackTrace();

      } finally {

         out.close();

         try {

            if (stmt != null) stmt.close();

            if (conn != null) conn.close();  // return to pool

         } catch (SQLException ex) {

             ex.printStackTrace();

         }

      }

   }

}

***********************

JSP showbooks.jsp code

**********************

[tomcat@sever33fedora ConnectionPool]$ cat showbooks.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/TestDB">

select id, title, author, price from books

</sql:query>

<html>

  <head>

    <title>DB Test</title>

  </head>

  <body bgcolor="C0C0C0">

  <h3>JSP displayes table content via tags "c:" and "sql:"</h3>

<table border='2'>

<c:forEach var="row" items="${rs.rows}">

   <tr><td> ${row.id} ;  ${row.title} ; ${row.author} ; ${row.price} </td></tr>

</c:forEach>

</table>

  </body>

</html>



























Saturday, January 16, 2021

Setting up Tomcat 9.0.41 && mariadb-server 10.5 on Debian Bullseye/sid

Setup Tomcat via original tar.gz ball  and JDK 11 allow to deploy "war" archive been built for Web Servlet Application from  http://lxer.com/module/newswire/view/295844/index.html . Thus CRUD Server side Java Apps might be moved oDebian Bullseye/sid in 3-5 minutes .

Creating account tomcat && download tar.gz ball 

$ sudo apt install default-jdk

$ sudo groupadd --system tomcat 

$ sudo useradd -d /opt/tomcat -r -s /bin/false -g tomcat tomcat

$ sudo apt -y install wget

$ export VER="9.0.41"

$ wget https://archive.apache.org/dist/tomcat/tomcat-9/v${VER}/bin/apache-tomcat-${VER}.tar.gz

Extracting tar.gz ball  to $HOME folder of tomcat

$ sudo mkdir /opt/tomcat

$ sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1

$ cd /opt/tomcat

$ sudo chown -R tomcat /opt/tomcat

$ sudo chmod -R g+r conf

$ sudo chmod g+x conf

$ sudo update-java-alternatives -l

java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64

 Creating Tomcat’s systemd service unit :-

 $ sudo vi /etc/systemd/system/tomcat.service

[Unit]

Description=Apache Tomcat Web Application Container

After=network.target

[Service]

Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid

Environment=CATALINA_HOME=/opt/tomcat

Environment=CATALINA_BASE=/opt/tomcat

Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh

ExecStop=/opt/tomcat/bin/shutdown.sh

User=tomcat

Group=tomcat

UMask=0007

RestartSec=10

Restart=always

[Install]

WantedBy=multi-user.target

Next

$ sudo systemctl daemon-reload

$ sudo systemctl start tomcat

$ sudo systemctl enable tomcat

Open port 8080 

$ sudo apt install ufw -y

$ sudo ufw allow 8080

Grant permissions to admin 

Update $CATALINA_HOME/conf/tom-users.xml

<tomcat-users>

. . .

    <user username="admin" password="password" roles="manager-gui,admin-gui"/>

</tomcat-users>

Update context.xml files under Tomcat’s  webapps  ( for remote access ) 

 <Context antiResourceLocking="false" privileged="true" >

  <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"

         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />-->

</Context>

$ sudo systemctl restart tomcat

`sudo apt install mariadb-server`  installs version 10.5 of mariadb on Debian Testing


Wednesday, January 13, 2021

Adding packaging and refactoring known sample of Java CRUD Application for Tomcat 9.0.41 ( Mysql 8.0.22)

Why packaging is important ? Original code was pretty straight  forward Java coding which looks and works quite smoothly . However , all compiled classes appear to be placed in one folder WEB-INF/classes/ and in some way this minor design issue hides a business logic of apps see https://www.javatpoint.com/crud-in-servlet . Once again Connection to mariadb-Server on F33 is encapsulated in separate class and matches the most recent requirements (8.0.22) . Code below is just a tutorial for Java Servlets code writing rather then competitor for JSF && JPA

Orinal code may be found at https://www.javatpoint.com/crud-in-servlet . 

Application is starting from HTML form 

[tomcat@server33fedora ServletCRUD0121]$ cat index.html

<!DOCTYPE html>  

<html>  

<head>  

<meta charset="ISO-8859-1">  

<title>Insert title here</title>  

</head>  

<body bgcolor='C0C0C0'>  

<h1>Add New Employee</h1>  

<form action="SaveServlet" method="post">  

<table>  

<tr><td>Name:</td><td><input type="text" name="name"/></td></tr>  

<tr><td>Password:</td><td><input type="password" name="password"/></td></tr>  

<tr><td>Email:</td><td><input type="email" name="email"/></td></tr>  

<tr><td>Country:</td><td>  

<select name="country" style="width:150px">  

<option>India</option>  

<option>USA</option>  

<option>UK</option>  

<option>Other</option>  

</select>  

</td></tr>  

<tr><td colspan="2"><input type="submit" value="Save Employee"/></td></tr>  

</table>  

</form>  

<br/>  

<a href="ViewServlet">view employees</a>  

</body>  

</html>  

[tomcat@server33fedora src]$ cat DBConnection.java

package mypackage.dao ;

import java.util.*;

import java.sql.*;

public class DBConnection {

 public static Connection getConnection(){

 String url = "jdbc:mysql://localhost/data_demo?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC";

    String username = "root";

    String password = "*******";

    System.out.println("In DBConnection.java class ");

    Connection con = null;

 try

  {

   try

   {

Class.forName("com.mysql.cj.jdbc.Driver").getDeclaredConstructor().newInstance();

    }

      catch (ClassNotFoundException e)

   {

            e.printStackTrace();

   }

   con = DriverManager.getConnection(url, username, password);

   }

     catch (Exception e)

  {

        e.printStackTrace();

  }

  return con ;

 }

}

[tomcat@server33fedora classes]$ cat  ../src/EmpDao.java

package mypackage.dao ;

import java.util.*;  

import java.sql.*;  

import mypackage.been.Emp;

import mypackage.dao.DBConnection ;

public class EmpDao {  

 public static int save(Emp e) {  

    int status=0; 

    String INSERT_EMP_SQL = "INSERT INTO emp2021(name,password,email,country) values (?,?,?,?)";  

    try(Connection con=DBConnection.getConnection(); PreparedStatement ps=con.prepareStatement(INSERT_EMP_SQL))

       {  

            ps.setString(1,e.getName());  

            ps.setString(2,e.getPassword());  

            ps.setString(3,e.getEmail());  

            ps.setString(4,e.getCountry());  

           status=ps.executeUpdate();  

           con.close();  

        } catch(Exception ex) { ex.printStackTrace(); }  

         return status;  

    }  

 public static int update(Emp e){  

   int status=0; 

   String UPDATE_EMP_SQL="update emp2021 set name=?,password=?,email=?,country=? where id=?" ; 

    try(Connection con=DBConnection.getConnection(); PreparedStatement ps=con.prepareStatement(UPDATE_EMP_SQL))

       { 

            ps.setString(1,e.getName());  

            ps.setString(2,e.getPassword());  

            ps.setString(3,e.getEmail());  

            ps.setString(4,e.getCountry());  

            ps.setInt(5,e.getId());  

            status=ps.executeUpdate();  

           con.close();  

        } catch(Exception ex) { ex.printStackTrace(); }  

      return status;  

    }  

 public static int delete(int id){  

   int status=0; 

   String DELETE_EMP_SQL="delete from emp2021 where id=?" ; 

   try(Connection con=DBConnection.getConnection(); PreparedStatement ps=con.prepareStatement(DELETE_EMP_SQL))

        {  

           ps.setInt(1,id);  

           status=ps.executeUpdate();  

           con.close();  

        } catch(Exception e) { e.printStackTrace(); }  

       return status;  

    }  

 public static Emp getEmployeeById(int id){  

  Emp e=new Emp();  

  String SELECT_EMP_SQL="select * from emp2021 where id=?" ;    try(Connection con=DBConnection.getConnection(); PreparedStatement ps=con.prepareStatement(SELECT_EMP_SQL))

       {  

         ps.setInt(1,id);  

         ResultSet rs=ps.executeQuery();  

         if(rs.next()){  

            e.setId(rs.getInt(1));  

            e.setName(rs.getString(2));  

            e.setPassword(rs.getString(3));  

            e.setEmail(rs.getString(4));  

            e.setCountry(rs.getString(5));  

        }  

            con.close();  

        } catch(Exception ex) {ex.printStackTrace();}  

      return e;  

    }  

 public static List<Emp> getAllEmployees() {  

   List<Emp> list=new ArrayList<Emp>();  

   String SELECT_ALL_SQL="select * from emp2021" ;        

  try(Connection con=DBConnection.getConnection();             PreparedStatement ps=con.prepareStatement(SELECT_ALL_SQL))

     {  

       ResultSet rs=ps.executeQuery();  

       while(rs.next()){  

        Emp e=new Emp();  

        e.setId(rs.getInt(1));  

        e.setName(rs.getString(2));  

        e.setPassword(rs.getString(3));  

        e.setEmail(rs.getString(4));  

        e.setCountry(rs.getString(5));  

        list.add(e);  

     }  

       con.close();  

     } catch(Exception e) { e.printStackTrace(); }  

   return list;  

 }  

}  

[tomcat@server33fedora src]$ cat ViewServlet.java

package mypackage.code ;

import java.io.IOException;  

import java.io.PrintWriter;  

import java.util.List;  

mport javax.servlet.ServletException;  

import javax.servlet.annotation.WebServlet;  

import javax.servlet.http.HttpServlet;  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse;  

import mypackage.dao.EmpDao ;

import mypackage.bean.Emp ;

@WebServlet("/ViewServlet")  

public class ViewServlet extends HttpServlet {  

    protected void doGet(HttpServletRequest request, HttpServletResponse response)   

       throws ServletException, IOException {  

        response.setContentType("text/html");  

        PrintWriter out=response.getWriter(); 

      out.println("<a href='index.html'>Add New Employee</a>");  

      out.println("<h1>Employees List</h1>");  

      List<Emp> list=EmpDao.getAllEmployees();  

      out.print("<body bgcolor='C0C0C0'><table border='1' width='100%'");  

        out.print("<tr><th>Id</th><th>Name</th><th>Password</th><th>Email</th><th>Country</th><th>Edit</th><th>Delete</th></tr>"); 

 for(Emp e:list)  {  

         out.print("<tr><td>"+e.getId()+"</td><td>"+e.getName()+"</td><td>"+e.getPassword()+"</td><td>"+e.getEmail()+"</td><td>"+e.getCountry()+"</td><td><a href='EditServlet?id="+e.getId()+"'>edit</a></td><td><a href='DeleteServlet?id="+e.getId()+"'>delete</a></td></tr>");  

        }  

        out.print("</table></body>");  

        out.close();  

    }  

}  

tomcat@server33fedora src]$ cat EditServlet.java

package mypackage.code ;

import java.io.IOException;  

import java.io.PrintWriter;  

import javax.servlet.ServletException;  

import javax.servlet.annotation.WebServlet;  

import javax.servlet.http.HttpServlet;  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse; 

import mypackage.dao.EmpDao ;

import mypackage.bean.Emp ;

 @WebServlet("/EditServlet")  

public class EditServlet extends HttpServlet {  

    protected void doGet(HttpServletRequest request, HttpServletResponse response)   

           throws ServletException, IOException {  

        response.setContentType("text/html");  

        PrintWriter out=response.getWriter();  

        // out.print("<html><body='C0C0C0'>");

        out.println("<h1>Update Employee</h1>");  

        String sid=request.getParameter("id");  

        int id=Integer.parseInt(sid);  

        Emp e=EmpDao.getEmployeeById(id);  

         out.print("<form action='EditServlet2' method='post'>");  

        out.print("<table>");  

        out.print("<tr><td></td><td><input type='hidden' name='id' value='"+e.getId()+"'/></td></tr>");  

        out.print("<tr><td>Name:</td><td><input type='text' name='name' value='"+e.getName()+"'/></td></tr>");  

        out.print("<tr><td>Password:</td><td><input type='password' name='password' value='"+e.getPassword()+"'/></td></tr>");  

        out.print("<tr><td>Email:</td><td><input type='email' name='email' value='"+e.getEmail()+"'/></td></tr>");  

        out.print("<tr><td>Country:</td><td>");  

        out.print("<select name='country' style='width:150px'>");  

        out.print("<option>India</option>");  

        out.print("<option>USA</option>");  

        out.print("<option>UK</option>");  

        out.print("<option>Other</option>");  

        out.print("</select>");  

        out.print("</td></tr>");  

        out.print("<tr><td colspan='2'><input type='submit' value='Edit & Save '/></td></tr>");  

        out.print("</table>");  

        out.print("</form>");  

        out.print("</html>");          

        out.close();  

    }  

}  

[tomcat@server33fedora src]$ cat EditServlet2.java

package mypackage.code ;

import java.io.IOException;  

import java.io.PrintWriter;  

import javax.servlet.ServletException;  

import javax.servlet.annotation.WebServlet;  

import javax.servlet.http.HttpServlet;  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse; 

import mypackage.dao.EmpDao ;

import mypackage.bean.Emp ;

@WebServlet("/EditServlet2")  

public class EditServlet2 extends HttpServlet {  

    protected void doPost(HttpServletRequest request, HttpServletResponse response)   

          throws ServletException, IOException {  

        response.setContentType("text/html");  

        PrintWriter out=response.getWriter();  

       String sid=request.getParameter("id");  

        int id=Integer.parseInt(sid);  

        String name=request.getParameter("name");  

        String password=request.getParameter("password");  

        String email=request.getParameter("email");  

        String country=request.getParameter("country");  

       Emp e=new Emp();  

        e.setId(id);  

        e.setName(name);  

        e.setPassword(password);  

        e.setEmail(email);  

        e.setCountry(country);  

       int status=EmpDao.update(e);  

        if(status>0){  

            response.sendRedirect("ViewServlet");  

        } else  {  

            out.println("Sorry! unable to update record");  

        }  

       out.close();  

    }  

}  

[tomcat@server33fedora src]$ cat DeleteServlet.java

package mypackage.code ;

import java.io.IOException;  

import javax.servlet.ServletException;  

import javax.servlet.annotation.WebServlet;  

import javax.servlet.http.HttpServlet;  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse;  

import mypackage.dao.EmpDao ;

@WebServlet("/DeleteServlet")  

public class DeleteServlet extends HttpServlet {  

    protected void doGet(HttpServletRequest request, HttpServletResponse response)   

             throws ServletException, IOException {  

        String sid=request.getParameter("id");  

        int id=Integer.parseInt(sid);  

        EmpDao.delete(id);  

        response.sendRedirect("ViewServlet");  

    }  

}  

[tomcat@server33fedora src]$ cat SaveServlet.java

package mypackage.code ;

import java.io.IOException;  

import java.io.PrintWriter;  

import javax.servlet.ServletException;  

import javax.servlet.annotation.WebServlet;  

import javax.servlet.http.HttpServlet;  

import javax.servlet.http.HttpServletRequest;  

import javax.servlet.http.HttpServletResponse; 

import mypackage.dao.EmpDao ;

import mypackage.bean.Emp ;

@WebServlet("/SaveServlet")  

public class SaveServlet extends HttpServlet {  

    protected void doPost(HttpServletRequest request, HttpServletResponse response)   

         throws ServletException, IOException {  

        response.setContentType("text/html");  

        PrintWriter out=response.getWriter();  

       String name=request.getParameter("name");  

        String password=request.getParameter("password");  

        String email=request.getParameter("email");  

        String country=request.getParameter("country");  

       Emp e=new Emp();  

        e.setName(name);  

        e.setPassword(password);  

        e.setEmail(email);  

        e.setCountry(country);  

       int status=EmpDao.save(e);  

      if(status>0){  

      out.print("<p>Record saved successfully!</p>");  

      request.getRequestDispatcher("index.html").include(request, response);  

        } else  {  

            out.println("Sorry! unable to save record");  

        }  

        out.close();  

    }  

}  

[tomcat@server33fedora src]$ cat Emp.java

package mypackage.bean ;

public class Emp {  

private int id;  

private String name,password,email,country;  

public int getId() {  

    return id;  

}  

public void setId(int id) {  

    this.id = id;  

}  

public String getName() {  

    return name;  

}  

public void setName(String name) {  

    this.name = name;  

}  

public String getPassword() {  

    return password;  

}  

public void setPassword(String password) {  

    this.password = password;  

}  

public String getEmail() {  

    return email;  

}  

public void setEmail(String email) {  

    this.email = email;  

}  

public String getCountry() {  

    return country;  

}  

public void setCountry(String country) {  

    this.country = country;  

}  

  

}  

Building ( I have some issues with Eclipse installed via snap and will make attempt to follow recent post at Fedora Magazine at my earliest convenience )

 javac -cp .:$CLASSPATH ../src/DBConnection.java -d  .

  javac -cp .:$CLASSPATH ../src/EmpDao.java -d  .

  javac -cp .:$CLASSPATH ../src/DeleteServlet.java -d  .

  javac -cp .:$CLASSPATH ../src/EditServlet.java -d  .

  javac -cp .:$CLASSPATH ../src/EditServlet2.java -d  .

  javac -cp .:$CLASSPATH ../src/SaveServlet.java -d  .

  javac -cp .:$CLASSPATH ../src/DeleteServlet.java -d  .

  javac -cp .:$CLASSPATH ../src/ViewServlet.java -d  . 

*************************************

Files layout under WEB-INF/classes/

*************************************

$ cd /opt/tomcat/latest/webapps/ServletCRUD0121/WEB-INF/src

[tomcat@server33fedora src]$ ll

total 32

-rw-r--r--. 1 tomcat tomcat  764 Jan 13 14:56 DBConnection.java

-rw-r-----. 1 tomcat tomcat  713 Jan 13 15:18 DeleteServlet.java

-rw-r-----. 1 tomcat tomcat 1462 Jan 13 15:23 EditServlet2.java

-rw-r-----. 1 tomcat tomcat 2113 Jan 13 15:24 EditServlet.java

-rw-r-----. 1 tomcat tomcat 3478 Jan 13 15:05 EmpDao.java

-rw-r-----. 1 tomcat tomcat  727 Jan 13 14:42 Emp.java

-rw-r-----. 1 tomcat tomcat 1439 Jan 13 15:23 SaveServlet.java

-rw-r-----. 1 tomcat tomcat 1511 Jan 13 15:23 ViewServlet.java

tomcat@server33fedora classes]$ ls -CR

.:

mypackage

./mypackage:

bean  code  dao

./mypackage/bean:

Emp.class

./mypackage/code:

DeleteServlet.class  EditServlet2.class  EditServlet.class  SaveServlet.class  ViewServlet.class

./mypackage/dao:

DBConnection.class  EmpDao.class


Mysql 8.0.22 table creation script :-

create database data_demo ;

CREATE TABLE emp2021 (

     id  int NOT NULL AUTO_INCREMENT,

     name varchar(20) ,

     password varchar(20) ,

     email varchar(20) ,

     country varchar(20) ,

     PRIMARY KEY (id) );