Show TOC Start of Content Area

Procedure documentation Developing the Web Front End  Locate the document in its SAP Library structure

Use

The presentation logic in this example is implemented by a simple HTML page and a servlet, which invokes the methods of the BusinessLogic class and shows the result to the user.

Prerequisites

You must have created the GettingStartedJDOWeb project.

You must have implemented the persistence and the business tiers of the application.

Procedure

Creating the HTML Page

       1.      In the J2EE Development perspective, open the context menu of the GettingStartedJDOWeb project.

       2.      Choose New   HTML. Enter index as the page name.

       3.      Choose Finish.

       4.      The HTML file opens automatically. In the Source tab enter the source of the page. It should contain forms for the following functions:

                            a.      Creating a new department

                            b.      Creating a new employee

                            c.      Selecting the employees from a department with a given ID

Here is the source of the HTML page:

Example

<html>

<head>

      <meta http-equiv= "Content-Type" content= "text/html" >

      <title> Getting Started With Java Persistence </title>

      <style type="text/css">

            h2 { text-align:left; }

            h4 { text-align:left; }

            h5 { text-align:left; margin-left:0; margin-right:0}

            .framed { border:solid 1px; }

            .narrow { margin-top:1px; margin-bottom:1px }

      </style>

</head>

<body>

      <h4 class="narrow"> SAP WEB APPLICATION SERVER </h4>

      <hr>

      <h2>Getting Started With Java Data Objects (JDO)</h2>

      <h4>Select an option and enter the relevant data. To confirm, use <i>Submit</i>. The department ID is <u>required</u> for all options.</h4>

      <h5 class="narrow">Recommended sequence:</h5>

      <blockquote>

      <h5 class="narrow">1. Create a department</h5>

      <h5 class="narrow">2. Create employees within the department</h5>

      <h5 class="narrow" style= "margin-bottom: 10px">3. List employees </h5>

      </blockquote>

      <form method= "POST" action= "ProcessInput" >

      <table class="framed" style= "background-color:lightblue" >

            <tr>

               <td class="framed">Department ID: <input type= "text" name= "DEPID" size= "10" value="1"></td>

            </tr>

            <tr>

                <td class="framed"><input type= "radio" value= "NEW_DEP" name= "ACTION"> New Department:</td>

             <td class="framed">

               <table>

                     <tr>

                         <td> Name:</td>

                         <td><input type= "text" name= "NAME" size= "30"></td>             

                     </tr>

               </table>

                </td>

            </tr>

            <tr>         

                <td class="framed"><input type= "radio" name= "ACTION" value= "NEW_EMP" > New Employee:</td>

                <td class="framed">

                  <table>

                        <tr>

                            <td>Employee ID:</td>

                            <td><input type= "text" name= "EMPID" size= "10"</td>

                        </tr>

                        <tr>

                            <td>First Name: </td>

                            <td><input type= "text" name= "FIRST_NAME" size= "30" ></td>

                        </tr>

                        <tr>

                            <td>Last Name: </td>

                            <td><input type= "text" name= "LAST_NAME" size= "30" ></td>

                        </tr>

                        <tr>

                            <td>Salary: </td>

                            <td><input type= "text" name= "SALARY" size= "10"></td>

                        </tr>

                  </table>

              </td>

            </tr>

            <tr>

                <td class="framed"><input type= "radio" name= "ACTION" value= "LIST" checked>List Employees</td>

            </tr>

      </table>

      <p><input type="submit" value="Submit" name="B3"></p>

     </form>

</body>

</html>

 

       5.      Save and close the file.

Creating the Servlet

...

       1.      In the J2EE Development perspective, choose GettingStartedJDOWeb project and open its context menu. Choose New Package.

       2.      Enter temp.persistence.gettingstarted.web as the name of the package. Choose Finish.

       3.      From the context menu of GettingStartedJDOWeb project choose New Servlet.

       4.      Enter ProcessInput as the servlet name. Choose HTTPServlet for the servlet type. To choose a package, use Browse… next to the Package field.

       5.      Select temp.persistence.gettingstarted.web from the list. Confirm the settings by choosing Finish.

       6.      The servlet class is created and opens automatically. Edit the contents of the file as follows:

                            a.      Implement the doGet() method. It must parse the input from the HTML page and invoke the relevant method of the BusinessLogic class:

Example

protected void doGet(

      HttpServletRequest request,

      HttpServletResponse response)

      throws ServletException, IOException {

 

   response.setContentType("text/html");

   PrintWriter out = response.getWriter();

   try {                   

      // the plain Java class BusinessLogic.java

      // encapsulates the business logic:

      BusinessLogic businessLogic = new BusinessLogic();

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

      out.println("<h4>Using Java Data Objects (JDO)</h4>");

  

      // read the user input

      // invoke the appropriate business logic functionality

      // generate the response (html) page             

      String action = request.getParameter("ACTION");

      if (action.equals("NEW_DEP")) {

         int depId = Integer.parseInt(request.getParameter("DEPID"));

         String depName = request.getParameter("NAME");

         businessLogic.createDepartment(depId, depName);

         out.println("Department \"" + depName + "\" created.");

 

      } else if (action.equals("NEW_EMP")) {

         int depId = Integer.parseInt(request.getParameter("DEPID"));

         int empId = Integer.parseInt(request.getParameter("EMPID"));

         String firstName = request.getParameter("FIRST_NAME");

         String lastName = request.getParameter("LAST_NAME");

         BigDecimal salary = new BigDecimal(request.getParameter("SALARY"));

 

         businessLogic.createEmployee(empId, firstName, lastName, salary, depId);

 

         out.println("Employee \"" + firstName + " " + lastName + "\" created.");

 

      } else if (action.equals("LIST")) {

 

         int depId = Integer.parseInt(request.getParameter("DEPID"));

 

         Employee[] emps = businessLogic.getEmployeesFromDepartment(depId);

 

         if (emps.length == 0) {

            out.println("<br>no data");

         } else {

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

            out.println("<td>Employee ID</td>");

            out.println("<td>First Name</td>");

            out.println("<td>Last Name</td>");

            out.println("<td>Salary</td>");

            out.println("<td>Department ID</td></tr>");

 

         for (int i = 0; i < emps.length; i++) {

            out.println("<tr>");

            out.println("<td>" + emps[i].getEmpId() + "</td>");

            out.println("<td>" + emps[i].getFirstName() + "</td>");

            out.println("<td>" + emps[i].getLastName() + "</td>");

            out.println("<td>" + emps[i].getSalary() + "</td>");

            out.println("<td>" + depId + "</td>");

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

         }

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

         }

 

      } else {

         out.println("Illegal action: " + action);

      }

 

   } catch (Throwable ex) { // catches any exception thrown

      out.println("Exception caught");

      out.println("<code>");

      ex.printStackTrace(out);

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

 

   } finally {

      out.println("<p><a href=index.html>Home</a><p>");

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

   }

}

 

                            b.      Implement the doPost() method. It must invoke doGet():

Example

protected void doPost(

   HttpServletRequest request,

   HttpServletResponse response)

   throws ServletException, IOException {

 

      doGet(request, response);

}

 

                            c.      To add the required imports, position the cursor anywhere in the Java editor and open the context menu. Choose Source Organize Imports. The following import declarations are added to the existing ones:

Example

import java.io.PrintWriter;

import java.math.BigDecimal;

 

import temp.persistence.gettingstarted.jdo.BusinessLogic;

import temp.persistence.gettingstarted.jdo.Employee;

 

       7.      Save and close the file.

Result

You have developed all components of the application. Now go on with its assembly.

 

End of Content Area