CS4025 Final Exam

 

9:00am – 12:00pm, Saturday, December 7, 2002

 

Please write your answers for total 5 questions in 5 pages in booklets.

 

1.     (40%) Consider the following very simple Web application system. The system first asks a user to enter her user id and password online, and displays a welcome/greeting message with the user’s real name, if the user has an account in the system. Otherwise, the system will display a welcome message, and ask the user to register as a new user by entering a user id and a password that the user chooses as well as the user’s real name. If the user id already exists in the system, the system asks the user to enter another user id; this step may be repeated several times until a distinct user id is entered. Then the system creates an account for the new user and displays the same welcome/greeting message as an existing user.

 

In this question, you need to design the above Web application in four different Web application architectures. For each architecture, you need to draw two UML diagrams to show the structure of the Web application using a class diagram, and collaboration/control-flow between components of the Web application using a sequence diagram.

 

In the class diagram, you need to model all components of the application as classes and relationships between the classes in the same way as you did in labs and assignments. For each class, you need to give all attributes without types and all operations/methods without signatures.

 

In the sequence diagram, you need to show the control flow and interaction of all the involved components by method calls from one to another. This diagram should show the following scenario. The user is a new user who first time visits the Web site and registers to the system. Assume that the user id that the user enters in the registration form does not already exist in the system.

 

      The following are the four different Web application architectures:

 

a)     (10%) 2-tiered: HTML/FORM – Servlet – DAO – Database

 

b)     (10%) 2-tiered: HTML/FORM – JSP (no JavaBean) – DAO – Database

 

c)     (10%) 3-tiered: HTML/FORM – Servlet – Session Bean – DAO - Database

 

d)     (10%) 3-tiered: HTML/FORM – Servlet –Session Bean – Entity Bean (CMP)

 

 

 

 

 

 

2.     (10%) Given the following simple HTML/FORM.

 

<html><head><title>Login Page</title></head>

<body>

<form action=”http://www.unb.ca/login.pl” method=”…”>

User Id: <input type=”text” name=”userid”> <br />

Password: <input type=”text” name=”passwd1”> <br />

Confirm Password: <input type=”text” name=”passwd2”> <br />

<input type=”submit”>

</form>

<body></html>

 

a)     (6%) Write the content of the request when the form is submitted to the Web server by the browser using each of the following two methods:

§       GET

§       POST

 

Assume HTTP version 1.1 is used. The submitted contents of the form’s text inputs are as follows:

 

User Id:

My user id

Password:

12&34%56

Confirm Password:

12&34%56

 

 

b)     (2%) Are the query strings submitted using GET and POST methods same or different? What is/are the query string(s) from the two different submissions.

 

c)     (2%) Where does the login.pl CGI/Perl program get the query string when the request is submitted by

 

a)     GET method

b)     POST method

 

 

 

 

 

 

 

 

 

 

 

 

 

3.     (15%) Given the following simple HTML/FORM

<html>

<head><title>Submit Page</title>

</head>

<body><form>

User Id: <input type=”text” name=”id”><br />

Total: $<input type=”text” name=”total”> <br />

<input type=”submit”>

</form></body></html>

 

      Add JavaScript code to the above HTML/FORM to check the followings:

 

a)     (3%) After the user enters user id, check if the user id consists of exact 10 digits. If not, ask the user reenter it again.

 

b)     (6%) After the user enters total amount, check if the entered amount is either a whole number or a decimal number with at most two digits after the decimal point. If not, ask the user reenter it again.

 

c)     (6%) When the submit button is pressed, double check a) and b), if they are not ok, ask the user to change them. If they are ok, confirm with the user that the user id and total amount that she entered are correct, and she would like to submit. If the user confirms, the submission will go to the server, otherwise the submission will be canceled.

 

 

 

4.     (15%) The following (next page) is a simplified version of the BookInfoHandlerBean session bean of Lab 7.

 

a)     (2%) Why does this session bean have to be declared as a stateful session bean?

 

b)     (6%) As a stateful session bean, the given code is not complete. Write additional code to complete this stateful session bean. Note that you do not need to copy the given code to your booklet. You just need to write additional code and indicate where the code goes in the given code.

 

c)     (7%) Can you convert this stateful session bean to an equivalent stateless session bean so that the change does not affect clients’ usages of the bean? If your answer is yes, re-write the bean as a stateless session bean. If your answer is no, explain why not.

 

 

 

 

 

 

 

public class BookInfoHandlerBean implements SessionBean{

  Connection dbcon = null;

  public void ejbCreate() throws EJBException {

    DataSource ds = null;

    String dbname = "java:comp/env/jdbc/yourid";

    try {

      InitialContext ic = new InitialContext();

      ds = (DataSource) ic.lookup(dbname);

    } catch (Exception ex) {

      throw new EJBException("Cannot get Data Source:" + ex);

    }

    openDB(ds);

  }

 

  public Vector computeCategories() throws EJBException {

    String sql = "select distinct category from books";

    Vector categories = new Vector();

    try {

      if (dbcon != null) {

        PreparedStatement pstmt = dbcon.prepareStatement(sql);

        ResultSet rs = pstmt.executeQuery();

        while (rs.next()) {

         String category = rs.getString(1);

         categories.addElement(category);

       }

       rs.close();

       pstmt.close();

      }

    } catch (Exception ex) {

      throw new EJBException("Cannot execute SQL statement:" + ex);

    }

    return categories;

  }

 

  public void ejbRemove() {}

  public void ejbActivate() {}

  public void ejbPassivate() {}

  public void setSessionContext(SessionContext sc) {}

 

  void closeDB() throws EJBException {

    try {

      dbcon.close();

      dbcon = null;

   } catch (Exception ex) {

     throw new EJBException("Cannot close the database connection.");

   }

  }

  void openDB(DataSource ds) throws EJBException {

    try {

      dbcon = ds.getConnection();

    } catch (Exception ex) {

      throw new EJBException("Cannot connect to database: " + ex);

    }

  }

 

}

5.     (20%) You are asked to write a simple Java Servlet to show the number of requests to this servlet, that is, how many times this servlet has been accessed by clients, for EACH of the following cases. Note that you can write a single servlet for all the cases, or for each case you write a separate servlet.

 

a)     (5%) The count of accesses to the servlet by all clients from any machines since the servlet started running.

 

b)     (5%) The count of accesses to the servlet by a particular client on a particular machine for a week (7 days or 7 * 24 hours).

 

c)     (5%) The count of accesses to the servlet by the current client within the same session.

 

d)     (5%) The count of accesses to the servlet by the submissions from the HTML/Form generated by this servlet.

 

The following is a sample template of the servlet. You can add and/or modify this code for your servlet(s). You do not need to copy the sample code to your booklet. You just need to indicate where to add your code or modify the sample code.

 

public class AccessCount extends HttpServlet {

  HttpServletRequest request;

  HttpServletResponse response;

  public void doPost(HttpServletRequest request,

     HttpServletResponse response) throws ServletException, IOException {

    this.request = request; this.response = response;

    controller();

  }

  public void doGet(HttpServletRequest request,

     HttpServletResponse response)throws ServletException, IOException {

     this.request = request; this.response = response;

     controller();

  }

  private void controller(){

    //add your code here

    generateCountPage(count, case);

  }

  private void generateCountPage(int count, String case) {       

    response.setContentType("text/html");

    try {

      writer = response.getWriter();

    } catch (Exception ex) {}  

    writer.println("<html><head>");

    writer.println("<title>Access Count</title></head> \n");

    writer.println("<body>");

    writer.println("This page has been accessed" + count + " times ");

    writer.println("in case: " + case + " <br />");

    writer.println("<form><input type=submit value=’Submit Again’>");

    writer.println("</form></body></html>");

  }

}