source: http://www.javapassion.com/handsonlabs/jspbasics/

Lab: 08


Objective

To implement a simple jsp application

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

  1. Double click index.jps under hello1->Web Pages to open in the source editor.
  2. Modify the code such that it asks you a question.
  3. Modify response.jsp under hello1->Web Pages such that it fetches the information sent from the client and displays on the screen

running sample

Exercise 2: Write an application that fetches the HTTP Headers sent by the browser

  1. Write a jsp page or modify the sample code such that it fetches the following headers from the client's browser and displays them in the form of an .html page.
    1. HTTP Method
    2. Request URL
    3. Protocol
    4. Client's IP Address
    5. Content type supported by the browser
    6. List of all headers sent by the browser

Exercise 3: 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 4: Write an application that accesses the MySQL database server

  1. Write a JSP page or modify the sample code such that it fetches all the country names along with their population from the MySQL database server (as done in the previous lab) and lists them in a table within an .html page.

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