OO Programming with Java


2.b. Dynamic parts are defined by the way of "Servlet" whose an illustration is defined below:

cat > WEB-INF/classes/Users.java << EOF
import java.io.*;
import java.util.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class Users extends HttpServlet {
  public void doGet(HttpServletRequest request
    , HttpServletResponse response) 
    throws IOException, ServletException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println(""+
"<html>"+
"  <head>"+
"    <title>UMS</title>"+
"    <link rel='stylesheet' href='./static/template.css'>"+
"  </head>"+
"  <body>"+
"    <h1>Users Management System</h1>"+
"    <ul>"+
"      <li><b>Name - Mark</b></li>");

      out.println("<li>"+
"      <form action='./Users' method='POST'>"+
"        <input name='name'></input>"+
"        <input name='mark'></input>"+
"        <button>add</button>"+
"      </form>"+
"      </li>"+
"    </ul>"+
"  </body>"+
"</html>");
  }
}
EOF

The servlet simply generates the code used in the "static" part but can now use variables, statements, other objets, etc. ! The space let in the code will be used as an illustration.

But, at this time, the code must be:

  1. Compiled with apache-tomcat-10.1.20/lib/* added to the CLASSPATH.
javac -cp ".:../../lib/*" WEB-INF/classes/*.java

5 - 16