Skip to main content.

Lab 6: JSPs

Goals

After completing this lab, you should be able to

Set Up

You'll continue using the ServletLab repository you set up and used in labs 4 and 5. Tag your code for Lab 5 as Lab5Submission so that you can continue to work and can easily roll back to the lab 5 version as needed.

Creating JSPs

Simple JSP (25 pts)

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 in the browser. 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 in your browser 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 use the fully qualified classname (specifically the "java.util" part) 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. Dealing with time is a common scenario in web application development. How can you test that your time-based logic is correct? (Make sure your final submission works, and is not just working for your test scenario.)

Modifying the Login Servlet (60 pts)

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. Make a new JSP that contains the login form. (Delete the original login.html file.) Update the link on index.html
  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.

Whew! Lots of refactoring! Reflect on the changes that you made. How is this code better, thinking about our *ilities? In what ways is it worse? Are you comfortable with those tradeoffs?

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.