OO Programming with Java


The preceding code is rather "complicated", isn't it ?
YES, and it's for this reason JSP were invented: rather than writing html in Java, write Java into html. The file extension is then .jsp and Java code is put into <% here %>.

  1. Swapping Java and HTML (JSP)

The preceding code can be re-written using JSP as follow:

cat > UMS.jsp <<EOF
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
  <%
  ArrayList<String>users = new ArrayList<String>();
  users.add("bill");
  users.add("kate");
  %>
<html>
  <head>
    <title>UMS</title>
    <link rel="stylesheet" href="./static/template.css">
	</head>
	<body>
    <h1>Users Management System</h1>
    <ul>
      <li><b>Name </b></li>
      <% for (String user : users) { %>
      <li> <%=user%>  </li>
      <% } %>
      <li><form action="./Users" method="POST">
        <input name="name"></input>
        <button>add</button>
      </form></li>
    </ul>
  </body>
</html>
EOF

Now, the application is available at: http://127.0.0.1:8080/web/UMS.jsp


8 - 16