Developing Web Components

Target Audience and Content

The target audience for this guide is the Web Component provider, i.e. the person in charge of developing the web components on the server side.

The content of this guide is the following:

  1. Target Audience and Content
  2. Introduction
  3. The JSPs
  4. The Servlets

Introduction

A Web Component is a generic term which specifies both the two kind of Web Components (JSPs and Servlets).

The JSPs

Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML written in Java programming language to encapsulate the logic that generates the content for the page. For more details see Java Server PagesTM and Quickstart guide.

Example:

The following example show a sample of a JSP which list the content of a cart.

    <!-- Get the session -->
    <%@ page session="true" %>

    <!-- The import to use -->
    <%@ page import="java.util.Enumeration" %>
    <%@ page import="java.util.Vector"      %>

    <html>
    <body bgcolor="white">
      <h1>Content of your cart</h1><br>
      <table>
        <!-- The header of the table -->
        <tr bgcolor="black">
          <td><font color="lightgreen">Product Reference</font></td>
          <td><font color="lightgreen">Product Name</font></td>
          <td><font color="lightgreen">Product Price</font></td>
        </tr>

        <!-- Each iteration of the loop display a line of the table -->
        <%
          Cart cart = (Cart) session.getAttribute("cart");
          Vector products = cart.getProducts();
          Enumeration enum = products.elements();
          // loop through the enumeration
          while (enum.hasMoreElements()) {
              Product prod = (Product) enum.nextElement();
        %>
        <tr>
          <td><%=prod.getReference()%></td>
          <td><%=prod.getName()%></td>
          <td><%=prod.getPrice()%></td>
        </tr>
        <%
        } // end loop
        %>
      </table>
    </body>
    </html>
    

The Servlets

Servlets are modules of Java code that run in an application server to answer client requests. Servlets are not tied to a specific client-server protocol but they are most commonly used with HTTP and the word "Servlet" is often used in the meaning of "HTTP Servlet".

Servlets make use of the Java standard extension classes in the packages javax.servlet (the basic Servlet framework) and javax.servlet.http (extensions of the Servlet framework for Servlets that answer HTTP requests).

Typical uses for HTTP Servlets include:

For more details see JavaTM Servlet Technology and Servlets tutorial.

Example:

The following example show a sample of a Servlet which list the content of a cart.

    import java.util.Enumeration;
    import java.util.Vector;
    import java.io.PrintWriter;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class GetCartServlet extends HttpServlet {

        protected void doGet(HttpServletRequest req, HttpServletResponse res)
                             throws ServletException, IOException {

            res.setContentType("text/html");
            PrintWriter out = res.getWriter();

            out.println("<html><head><title>Your cart</title><head>");
	    out.println("<body>");
	    out.println("<h1>Content of your cart</h1><br>");
	    out.println("<table>");

            // The header of the table
	    out.println("<tr>");
            out.println("<td><font color="lightgreen">Product Reference</font></td>");
	    out.println("<td><font color="lightgreen">Product Name</font></td>");
	    out.println("<td><font color="lightgreen">Product Price</font></td>");
            out.println("</tr>");

            // Each iteration of the loop display a line of the table
            HttpSession session = req.getSession(true);
            Cart cart = (Cart) session.getAttribute("cart");
	    Vector products = cart.getProducts();
	    Enumeration enum = products.elements();
	    while (enum.hasMoreElements()) {
	        Product prod = (Product) enum.nextElement();
	        int prodId = prod.getReference();
                String prodName = prod.getName();
                float prodPrice = prod.getPrice();
	        out.println("<tr>");
	        out.println("<td>" + prodId + </td>);
	        out.println("<td>" + prodName + </td>);
	        out.println("<td>" + prodPrice + </td>);
	        out.println("</tr>");
            }

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