Skip to main content.

Lab 6: JSPs

Page Contents:

Goals

After completing this lab, you should be able to

Set Up

Create a new Dynamic Web Project named Lab6.

Create a index.html page that will contain links to the other pages that you develop.

Creating JSPs

Simple JSP (25 pts)

Next, create a simple JSP.

  1. Create a new JSP named "first.jsp", click Finish.

    Note that this file looks very much like a typical HTML file with a new first line.

  2. Add a title and a simple heading such as "My First JSP"
  3. Run the project on the server and view the page in a browser. The page should show up, although it may take a little while to compile the JSP into a servlet and then execute. On subsequent loads, it should be faster.
  4. Make a small change to, say, the heading. Then, simply reload the web page. You should see the change you made--without having to restart the server.
  5. Now, we'll add dynamic content to the JSP. Add a paragraph tag with the message The time is now <%= new java.util.Date() %>
  6. Reload the JSP page and see the dynamic content. Reload the page again--you should see the new current time.
  7. Create a new Date variable and display it.
    <% java.util.Date date = new java.util.Date(); %>
    <p>The time is now <%= date %></p>

    Make sure the Server has synchronized (you'll see a message on the Server within Eclipse's console) and reload your JSP.

  8. Copy this file and save it into your WebContent directory.
  9. Using the JSP include directive, include that file in the JSP's header (<head>).

    Review: Why should that (particular) file be in the page's (<head>)?

    Make sure the Server has synchronized and reload your JSP.

  10. Modify the declaration for the date variable so that it doesn't need to say "java.util" by importing the package java.util.* using the import directive (see slides).

    Make sure the Server has synchronized and reload your JSP.

  11. Modify the JSP so that it displays "Good morning!" if the current hour of the day is greater than 3 but less than 12, prints "Good afternoon" if the current hour of the day is greater than or equal to 12 and less than 17, and prints "Good night!" otherwise.

    You should create and use a GregorianCalendar object and its methods/inherited fields to perform the above logic.

    This is hard to test because it is time-based. That's a not-uncommon scenario. How can you test that your time-based logic is correct?

Modifying the Login Servlet (60 pts)

Copy your Login-related files (the HTML and servlet files) from Lab 5 into Lab 6.

Objective: Modify your LoginServlet to direct a request to one of two JSPs.

  1. Create a Congratulations response in a JSP. Where should the JSP located, since it should only be accessed through a servlet?
  2. Include the style.txt file in the congratulations JSP too.
  3. Convert the login form into a JSP. (Delete the original login.html file.)
  4. Modify your LoginServlet to, instead of generating the HTML responses, forward the request to the appropriate JSP response (i.e., either to the congratulations page or back to the login page). The rest of the behavior should remain the same.
    1. Modify your LoginServlet to create an error message that is stored in the request object as an attribute. There is one error message if the username wasn't entered ("Must enter a username"), a different error message if the password wasn't entered ("Must enter a password"), and another error message if the username and password don't match ("Username or password isn't correct.") Note that the attribute can have the same name, regardless of the content/value of the attribute.
    2. Modify the login JSP to display the error message (only if the error message exists). Display the error message in an appropriate location and style, since it is an error message. (Think about what error messages--especially login errors--tend to look like and where they are placed on the web page.)
    3. Test that it is behaving correctly.
    4. Confirm that the authentication is still working as well (i.e., you can still access the promised land if the username/password are correct).

Objective: Modify AuthenticatedTest to direct request to a JSP.

  1. Create a new authenticated JSP called authenticated.jsp (where should this file be?) and fill it in with this code. You may have to make some changes so that this code works with your code, but it gives you a start.
  2. Update AuthenticatedTest so that it directs the user to authenticated.jsp if the user is authenticated. (The error page generated through the servlet is fine.)
  3. Test that everything works, end to end/start to finish with correct and incorrect passwords.

    This isn't too interesting. We're just setting up for something in a later lab.

index.html

If you haven't already, add links from index.html to the pages you created in this lab.

Submission

Export the project as a .war file named "Lab6.war" and export the source files. Copy the .war file into your GitHub repository (still reusing the one for lab 4).

Grading (85 pts)

This lab is due tonight at 11:59.