Lab 6: JSPs
Page Contents:
Goals
After completing this lab, you should be able to
- Write simple JSPs
- Refactoring code and coordinating between Servlets and JSPs
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.
- 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.
- Add a title and a simple heading such as "My First JSP"
- 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.
- 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.
- Now, we'll add dynamic content to the JSP. Add a paragraph tag with the message The time is now <%= new java.util.Date() %>
- Reload the JSP page and see the dynamic content. Reload the page again--you should see the new current time.
- 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.
- Copy this file and save it into your WebContent directory.
- 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.
- Modify the declaration for the
date
variable so that it doesn't need to say "java.util" by importing the packagejava.util.*
using theimport
directive (see slides).Make sure the Server has synchronized and reload your JSP.
- 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.
- Create a Congratulations response in a JSP. Where should the JSP located, since it should only be accessed through a servlet?
- Include the
style.txt
file in the congratulations JSP too. - Convert the login form into a JSP. (Delete the original login.html file.)
- 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.
- 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. - 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.)
- Test that it is behaving correctly.
- 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.
- 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. - Update
AuthenticatedTest
so that it directs the user toauthenticated.jsp
if the user is authenticated. (The error page generated through the servlet is fine.) - 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.
- Simple JSP (25 pts)
- Login with JSPs (60 pts)