Lab: 09


Objective

To implement a simple jsp application while maintaining the application logic in utility classes

Implementation

Exercise 1: Build and run HelloWorld JSP Application

  1. Download the sample code.
  2. Open hello1 NetBeans project. 
  3. Build (F11) and run (F6) hello1 project.

running sample

Exercise 2: Add customized error pages to the web application

  1. Add a jsp page named myErrorPage to hello1->Web Pages.
    1. Right click WebPages and select New->JSP.
    2. Under New JSP File dialog box, for JSP File Name: field, type in myErrorPage.
    3. Mention that the file is to be created in the folder error .
    4. Click Finish. 

running sample

  1. Modify the default page to display your error page.
    1. Note the directiive <%@ page isErrorPage="true" %>
  2. <html>
    <body bgcolor="red">
        <%@ page isErrorPage="true" %>
        <h1> This is my own error page and the exception <%= exception.getMessage() %> tells me you
             made a wrong choice.
    </body>
    </html
  3. Add the directive <%@ page errorPage="error/myErrorPage.jsp" %> at the top of the page which you want to handle the error.
  4. In case of any exception thrown while your jsp page is executed, the container will execute the error page (if specified).

running sample

Exercise 3: Write an application that accesses the MySQL database server

  1. Add a class to the web application named DBManager to hello1->Web Pages.
    1. Right click hello1 and select New->Java Class .
    2. Under New Java Class dialog box, for Class Name: field, type in DBManager.
    3. Mention that the file is to be created in the Source Packages named lab09.
    4. Click Finish. 
  2. running sample

  3. Modify/Add the required methods to the class DBManager such that it has the functionality of returning the population density of countries in a specified continent.
  4. Write a JSP page or modify the sample code such that it creates a table of all the countries, their population, area and population density within the continent specified by the user.
    1. Note that to use the code, you would be required to import the package by adding the directive <%@page import="lab09.DBManager" %>

Exercise 4: Modify the code such that it returns the required data in the form of an excel sheet

  1. Modify exersice 2 in such a way that the results are either returned as html or in the form of an excel sheet.
    1. Following is a sample jsp page that returns tabulated data in the form of an excel sheet when accessed with a URL such as: http://server/compare.jsp?format=excel.
    <html>
    <head>
    <title>Comparing Apples and Oranges</title>
    </head> <body>
    <center>
    <h2>Comparing Apples and Oranges</h2>
    <%
    String format = request.getParameter("format");
    if ((format != null) && (format.equals("excel"))) {
    response.setContentType("application/vnd.ms-excel");
    }
    %>
    <TABLE BORDER=1>
    <TR><TH></TH> <TH>Apples<TH>Oranges
    <TR><TH>First Quarter <TD>2307 <TD>4706
    <TR><TH>Second Quarter<TD>2982 <TD>5104
    <TR><TH>Third Quarter <TD>3011 <TD>5220
    <TR><TH>Fourth Quarter<TD>3055 <TD>5287 </TABLE>

    </center></body></html>

running sample

running sample

Exercise 5: Implement an application that utilizes session tracking

  1. Modify exersice 4 in such a way that the user has to login to access the list of all countries.
    1. Create a login jsp page that asks the username for his/her password.
    2. Store that information in the session object.
    3. Direct the user to the jsp page that lists the countries.
      1. If the user is logged in (i.e. the session objects maintain the correct password) then list the countries.
      2. Otherwise, redirect the user to the error page.

Instructions for submitting your work

  1. You are required to demonstrate your implementation during the lab.
  2. Create an archive (.zip) of your project folder and submit it online.

Last updated: 3rd December, 2007