Lab 4: Servlet Configuration and Session State
Goals
At the completion of this lab, you should be able to
- Implement
initanddestroymethods - Use configuration parameters
- Use sessions and session attributes
Set up
Create a new Dynamic Web Project called Lab4.
Adding init and destroy Methods (45 pts)
Copy pet.html and SurveyServlet.java from their
current location to the Lab4 project.
SurveyServlet is not implemented very efficiently.
The 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", select "Override/Implement Methods".
- In the
GenericServletclass selectinitanddestroy, 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, andtotalVotesso that they are instance variables for the class rather than temporary variables for thedoGetmethod. - Move the code that reads in the file into the
initmethod. Callsuper.init()first. You will have to handle some compiler errors appropriately. - Move the code that writes the file into the
destroymethod. Callsuper.destroy()last. You will have to handle some compiler errors appropriately. - Stop and restart your server.
- Test your servlet several times.
- Stop and restart your server.
- Test again. Still works, 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 FirstServlets project.
You should see the .class files for each of your servlets. The
build directory and the contents of
the WebContent directory are what are deployed on the
server.
Configuration Parameters (30 pts)
Sometimes, you want to be able to easily configure your servlets. For example, you want want to tell the servlet the location of a file or the parameters to a database or the email address of the administrator.
- Modify the
SurveyServletto include an init parameter. The init parameter's name issurveyFileand the value issurvey.dat.You can modify the
web.xmlfile 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. - Modify
SurveyServlet'sinitmethod to access the init parameter. (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.
Using Sessions and Session Variables (25 pts)
Copy your Login servlet and form from Lab 3 into this project. (Remember the appropriate locations for both of these files.)
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
true value. (Note
that this must be a Boolean object and not a
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 into
your
WebContentdirectory - Create a new servlet in the
servletspackage namedAuthenticatedTest - Copy this code into the
doGetmethod of the servlet you just created (AuthenticatedTest). Look at the code to understand what it is supposed to do. - 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.
Organization
Create an index page that links to the pet survey, the login page, and the authentication page.
Turnin
Create a .war file from your Lab4 project and copy it
into your turnin directory. Make sure to include the
source code.
Grading (100 pts)
- SurveyServlet: init, destroy (45 pts)
- SurveyServlet: configuration parameter (30 pts)
- LoginServlet: sessions (25 pts)