Lab 5: Servlet Configuration and Session
Goals
At the completion of this lab, you should be able to
- Implement
init
anddestroy
methods - Use configuration parameters
- Use sessions and session attributes
Set up
You'll continue using the Servlet Lab repository you set up for Lab 4.
Tag your code for Lab 4 as Lab4Submission
so
that you can continue to work and can easily roll back to the Lab
4 version as needed.
- Copy
pet.html
andSurveyServlet.java
from their current location to theServletLab
project. Copy the relevant part ofweb.xml
if needed. - Add link to
pet.html
inindex.html
.
Adding init and destroy Methods (37 pts)
SurveyServlet
is not implemented very efficiently.
The results file has to be read and written every time a vote is cast,
i.e., every time a request comes in. We don't want that overhead
because we expect this survey to be taken a lot! (Not really but...)
Add init
and destroy
methods to
SurveyServlet
. init
will read the file
and initialize the votes. destroy
will write the votes
out to the file. More detailed instructions follow:
- First, recall that you can use the "Refactor" and "Source" menus of Eclipse to quickly do trivial tasks.
- In the
SurveyServlet.java
editor pane, right-click, select "Source", and then "Override/Implement Methods". - Under the
GenericServlet
class, selectinit
anddestroy
, select where to put them (I put them below the constructor), and then click "OK". These method stubs will be automatically generated for you. - Move the
variables
surveyData
,votesForAnimals
, andtotalVotes
so that they are instance variables for the class rather than temporary/local variables for thedoGet
method. - Move the code that reads in the file into the
init
method. Keep the call tosuper.init()
first. You will have to handle some compiler errors appropriately. Handle errors in a thoughtful way--not like you're just telling Eclipse to fix them all and not thinking about the cleanliness of the resulting code. - Move the code that writes the file into the
destroy
method. Make the call tosuper.destroy()
last. You will have to handle some compiler errors appropriately. Handle errors in a thoughtful way--not like you're just telling Eclipse to fix them all and not thinking about the cleanliness of the resulting code. - Stop and restart your server in the Server view.
- Test your servlet several times.
- Stop and restart your server.
- Test again. It's using the updated data, right? Good!
Web Application Structure and Deployment
build Directory
Within a terminal (not within Eclipse), look at the contents of the
build
directory inside of your First
project. You should see the .class
files for each of
your servlets, within the classes/servlets
(or whatever
package name you used). The
build
directory and the contents of
the src/main/webapp
directory are what are deployed on
the server.
Eclipse Web Tools Platform (WTP)
Typically, web applications are put in
Tomcat's webapps
directory to be deployed. The Eclipse
WTP handles putting your files in an appropriate location.
Within a terminal, if you go into your workspace, and then from
there go into
.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Lab5
,
you should see your .html and .css files as well as
a META-INF
and WEB-INF
directory. Note that
you won't see the .metadata
directory because it is
hidden because it starts with a .
. To view all the
files, use the command-line option -a
, e.g.,
Go into the WEB-INF directory. You should see
a classes
directory and a lib
directory and,
perhaps, a web.xml file. The classes
directory contains
the servlets you wrote. The lib
directory contains any
jar files that you imported for your applications (beyond the standard
Java EE classes).
Configuration (25 pts)
Sometimes, you need to configure your servlets. For example, you want to tell the servlet the location of a file or the parameters to a database or the email address of the administrator.
URL Mapping
- Modify the
LoginServlet
such that its@WebServlet
annotation for the URL mapping is/login
instead of/LoginServlet
- Modify the login form to send requests (its
action
) tologin
instead ofLoginServlet
- Test that the login functionality still works as expected.
Init Parameters
- Modify the
SurveyServlet
to include an init parameter. The init parameter's name issurveyFile
and the value issurvey.dat
.You can modify the
web.xml
file or add the annotations toSurveyServlet
. To see examples of annotations, look at the slides or use Eclipse to create a new servlet and generate an example annotation for you. To create aweb.xml
file, you can right-click on the project, go to JavaEE Tools, and then selectGenerate Deployment Descriptor Stub
- Modify
SurveyServlet
'sinit
method to access the init parameter and refactor your code to use it. (Look at the methods that SurveyServlet inherits from HttpServlet). - Stop and restart the server.
- Test that the code still works correctly.
- Stop the server. Modify the parameter so that the
SurveyServlet's init parameter has a different file name, such
as
survey2.dat
. - Restart the server and test your code. You should not get the data you had before: it should be all 0s, except for whatever you just voted for.
You modify web.xml from the Source or the Design view, which ever
you are more comfortable with. In Design view, you can right click
on the SurveyServlet's servlet
element and select "Add
Child" and add "init-param". If you expand init-param
,
you'll see the two elements that you need to fill in. Click on
"Source" view to see the result. I want you to be very comfortable
with XML files.
Using Sessions and Session Variables (23 pts)
Commit your code and push to the GitHub repository. This will be the last reminder that you should do that (in this or any future lab).
Modify your Login servlet so that, if the user's name and password are
correct, it starts a new HttpSession
and stores an
attribute with name authenticated
and value a
Boolean
object that has a true
value. (Note
that this must be a Boolean
object and not a
boolean
. Why? What is the difference
between boolean
and Boolean
?)
Also set your session's maximum inactive interval to 60 seconds.
To test that your servlet is working correctly:
- Copy this form
and this image in
the
src/main/webapp
directory. (In the browser, you can say to "Save as" and put them in the appropriate location.) - Update your
index.html
file to have a link to your authentication test. - Create a new servlet in the
servlets
package namedAuthenticatedTest
- Copy this code into the
doGet
method of the servlet you just created (AuthenticatedTest). Look at the code to understand what it is supposed to do. Handle compiler errors appropriately. - Restart your server.
- Try clicking the button of the authenticate test form. You should see a message that says that you're not authenticated.
- Login, using your login form copied from last lab.
- Try clicking the button of the authenticate test form again. You should now see a message that you are authenticated.
- Wait 60 seconds and test the authenticate test form again. You should no longer be authenticated because the session expired.
Submission
Create a .war file from your Lab5
project named--you guessed it--Lab5.war
and copy it
into your git repository. Make sure to include the
source code.
Grading (90 pts)
Due tonight at 11:59 p.m.
- SurveyServlet: init, destroy (37 pts)
- LoginServlet: URL mapping (5 pts)
- SurveyServlet: configuration parameter (20 pts)
- LoginServlet: sessions (23 pts)
- index.html (5 pts)