Friday, September 18, 2026

Debian 13: jakarta tomcat create servlet manually

under directory WEB-INF create 2 folder

  1. src
  2. classes 

under src, we place servlet and java code.

for package com.dedetok, create folder ~/WEB-INF/src/com/dedetok/ and write 2 files

MyWorld.java

package com.dedetok;

public class MyWorld {

    public String getMessage() {
        return "hello world";
    }
}

HelloServlet.java 

package com.dedetok;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException {

        MyWorld world = new MyWorld();

        response.setContentType("text/plain");
        response.getWriter().println(world.getMessage());
    }

We map our servlet, edit ~/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                             https://jakarta.ee/xml/ns/jakartaee/web-app_6_1.xsd"
         version="6.1">

    <display-name>My Application</display-name>

    <servlet>
        <servlet-name>Hello Anything</servlet-name>
        <servlet-class>com.dedetok.HelloServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Hello Anything</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Note: <servlet-name> between <servlet> and <servlet-mapping> must match.

compile your java, go it ~/WEB-INF/ folder and run

$ javac   -cp /home/dedetok/apache-tomcat-11.0.26/lib/servlet-api.jar   -d ./classes   ./src/com/dedetok/*.java 

restart your jakarta tomcat and try servlet http://localhost:8080/myapp/hello. hello comes from <servlet-mapping>

It is not practicable to map every servlet, there is other way

now at directory ~/WEB-INF/src/com/dedetok/  create HelloServlet2.java

package com.dedetok;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import jakarta.servlet.annotation.WebServlet;

@WebServlet("/hello2")
public class HelloServlet2 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException {

        MyWorld world = new MyWorld();

        response.setContentType("text/plain");
        response.getWriter().println(world.getMessage()+" 2");
    }
}  

we use  @WebServlet("/hello2") to map url. do not forget to import  jakarta.servlet.annotation.WebServlet.

compile it, and restart your jakarta tomcat. you can access it via http://localhost:8080/myapp/hello2.

To use MyWorld class into jsp, at directory ~/myapp/ (upper directory from WEB-INF) , create file callservlet.jsp

<%@ page import="com.dedetok.MyWorld" %>

<%
    MyWorld world = new MyWorld();
%>

<html>
<body>
    <h1><%= world.getMessage() %></h1>
</body>
</html> 

or use wildcard for import="com.dedetok.*"  

Now you can directly build your application. MVC model can be used if you like. happy coding.  

 

Thursday, September 17, 2026

Debian 13: jakarta tomcat add application myapp manually without war (part 2)

It this part: 

  1. Add myapp
  2. Add mariadb library

Add myapp

  1. under folder <home>/webapps/ add folder myapp
  2. under folder <home>/webapps/myapp/  add folder WEB-INF
  3. optional, at folder <home>/webapp/myapp> write index.html e.g  
<!DOCTYPE html>
<html lang="en">
<body>
    <h1>Hello, World!</h1>
</body>
</html>

Restart your jakarta tomcat, and myapp appear in list of application. Test your index.html, url http://localhost:8080/myapp/.

Add mariadb 

  1. Under folder  <home>/webapps/myapp/WEB_INF, create folder lib.
  2. download jconnect from https://mariadb.org/connector-java/all-releases/, and put it in <home>/webapps/myapp/WEB_INF/lib, i.e. <home>/webapps/myapp/WEB-INF/lib/mariadb-java-client-3.5.3.jar

Under folder  <home>/webapps/myapp/ write test.jsp

<%
try {
    Class.forName("org.mariadb.jdbc.Driver");
    out.println("MariaDB JDBC driver FOUND");
} catch (ClassNotFoundException e) {
    out.println("MariaDB JDBC driver NOT FOUND");
}
%>

restart your jakarta tomcat and test it http://localhost:8080/myapp/test.jsp.

if you have mariadb running, you can extend test.jsp to this

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>

<%
try {
    Class.forName("org.mariadb.jdbc.Driver");
    out.println("MariaDB JDBC driver FOUND<br>");

    Connection conn = DriverManager.getConnection(
        "jdbc:mariadb://localhost:3306/mydb", // change to your db name
        "admin", // change to your user
        "admin" // change to your password
    );

    out.println("Connected to MariaDB!<br>");

    conn.close();

} catch (ClassNotFoundException e) {
    out.println("MariaDB JDBC driver NOT FOUND<br>");
    out.println(e);

} catch (Exception e) {
    out.println("Database connection FAILED<br>");
    out.println(e);
}
%> 

 

 

 

Debian 13: jakarta tomcat & openjdk 21 installation (part 1)

Assume openjdk 21 installed, it is lts verssion.

Download latest jakarta tomcat from https://tomcat.apache.org/download-11.cgi, i.e. version 11.0.26 (currently).

extract it into your home (user) directory

change permission to start jakarta tomcat

$ cd apache-tomcat-11.0.26/bin/
$ chmod 744 *.sh

Directory structure is:

  • bin
  • conf
  • lob
  • logss
  • temps
  • webapps
  • work 

You must add user to manage your jakarta tomcat. User rules are:

  1. manager-gui - allows access to the HTML GUI and the status pages
  2. manager-script - allows access to the text interface and the status pages
  3. manager-jmx - allows access to the JMX proxy and the status pages
  4. manager-status - allows access to the status pages only 

Edit <tomcat_home>/conf/tomcat-users.xml

<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
...
  <role rolename="manager-gui"/>
  <user username="admin" password="admin" roles="manager-gui"/>
</tomcat-users> 

To run jakarta tomcat

$ ./startup.sh&
[1] 6430
~/apache-tomcat-11.0.26/bin$ Using CATALINA_BASE:   <home>/apache-tomcat-11.0.26
Using CATALINA_HOME:   <home>/apache-tomcat-11.0.26
Using CATALINA_TMPDIR: <home>/apache-tomcat-11.0.26/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       <home>/apache-tomcat-11.0.26/bin/bootstrap.jar:/home/dedetok/apache-tomcat-11.0.26/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

[1]+  Done                    ./startup.sh

Open your browser and open url http://localhost:8080/. 

To shutdown, run $ ./shutdown.sh.

Your application will on <home>/webapps and root for http://localhost:8080 is on <home>/webapps/ROOT/

For production, go to "List Application", Undeploy any unused applications.