26 July 2012

Web components: extra exercises

Module 3 Exercise 2 (extra): a DAO model

  1. Extend the Anniversary class with an extra attribute: flowers
  2. Add the following files with a new model to the project: AnniversaryMemoryDao with interface AnniversaryDao
  3. Modify the  AnniversaryControl servlet
    1.  Instantiate an AnniversaryDao
      AnniversaryDao dao = AnniversaryMemoryDao.getAnniversaryDAO();
    2. Use anniversaryDao as the model
  4. Modify the JSP to show the flowers you get at the anniversary
  5. (Optional) Create a trivial unit test for the model
    1. Right click the project and select New > Other... > Unit TestsTest Packages  > JU Test for existing class
      1. Browse to the AnniversaryDao class
      2. Click Finish
    2. In the generated Test class
      1. Remove the TestGetAnniversaryDao class
      2. In the testGetByYear method
        1.  Create an AnniversaryDao instance like you did in the Controller servlet
        2. Supply an anniversary year to the getByYear method
        3. Modify the assertEquals method
          1. As a first argument type the name of the flowers matching the anniversary year
          2. As a second argument call the getFlowers method
        4. Add another assertEquals method testing the material
        5. Remove the fail method at the end of the test
      3. In the project window, right click the AnniversaryMemoryTest class and select Test File

Module 4 Exercise 2 (extra): working with sessions

1.      Add a “Set colour preferences” link to the Anniversary application index page
a.    The link should take the user to a colourpicker web page.
2.      Create the ColourPicker web page, that shows a drop down list with some of these colours:
a. Aqua Black Blue Fuchsia Gray Green Lime Maroon Navy Olive Purple Red Silver Teal Yellow
  (these are HTML color names defined in the HTML 3 standard)
b.   Add an OK button that takes you to the ColourControl servlet
3.      Create the ColourControl servlet. In the servlet
  1. Save the colour in the session
  2. Go back to the home page
4.      On the showAnniversary.jsp, show the text in the chosen colour. If there is no chosen colour, use black. 

 Hints:

HTML font tag example: <font color=”Black">  See the difference? </font>
EL ternary expression example ${aVariable == “”?”empty”:bVariable}

Module 5 Exercise 2 (extra): Default Colour

  1. Specify an initialization parameter with a default colour for the ColourControl servlet.
  2. Save the value in a session attribute
  3. Use the session attribute to set this colour as the default selected colour in the drop down list in the colourpicker page
  4. Test your application.
  5. The first time you go to the page the default colour is not selected. If you choose a colour and go back to the page the default colour is selected. Explain this behaviour.

Hint: HTML to set an option as the selected option:

  <option selected>Peach</option>

Module 6 Exercise 1 Task 2 (extra): Display multivalued headers

Write a version of the HTTP Headers list that is capable of displaying headers with multiple values

Module 6 Exercise 3 (extra): Call methods from EL

  1. Add a method calculateWeddingYear() to the Anniversary class in the wedding anniversary project.
  2.  Add a message to showAnniversary.jsp: You were married in the year xxxx!
  3. Test the project and correct any errors until you have the expected result.
  4. Optional: If the AnniversaryMemoryDao does not have an anniversary (e.g. 100) in its list it will return an Anniversary object where the material is empty. Show the message with material and flowers only if you have found an Anniversary object with a material. Show the message with the year of marriage only if you have found an Anniversary object with an empty material.

 

Module 8 Exercise 3 (extra): Count headers

Change Module 6 exercise 1 to display the number of headers on top of the list.

 

Module 8 Exercise 4 (extra): Colourpicker

Modify the colourpicker in the anniversary project:
  1. supply the colours as a comma separated string: aqua,black,blue,fuchsia gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow.
  2. generate the choice menu using a foreach loop
  3. Optional: When you return to the picker the previously chosen color should be selected by default.

 

Module 8 Exercise 5 (extra): Write a jspx document

  1. Copy index.jsp from Module 6 exercise 1 to index.jspx.
  2. Modify the code to be compliant with a jspx document.
Solution.

 

Module 10 Exercise 2 (extra): AnniversaryJpaDao

In the wedding anniversary project, add a AnniversaryJpaDao.
  1. Create the AnniversaryJpaDao class that implements the AnniversaryDao interface
    1. Add a constructor that takes an EntityManagerFactory as an argument.
    2. Implement the interface methods
      1. In the methods create an EntityManager:
        Entitymanager em = emf.createEntityManager();
      2. Use the EntityManager to access the database.
      3.  Adapt the AnniversaryControl servlet to use the AnniversaryJpaDao.
      4. Inject an EntityManagerFactory in the AnniversaryControl
        @PersistenceUnit private EntityManagerFactory emf;
      5. When the servlet is created, create the AnniversaryJpaDao and assign it to an AnniversaryDao attribute.
      6. Remove any reference to other AnniversaryDao implementations.
  2. Change the Anniversary class into a JPA Entity. 
    1. Use the year as a primary key.
  3.  Create a persistence unit in the project, use an existing glassfish DB resource
  4. Run the project.
    1. Ignore any errors that complain that the Anniversary table already exists.
    2. Correct other errors
  5. Enter the flowers entries into the database
    1.  Collect database characteristics
      1. In the services tab, right click Servers> GlassFish Server> Resources> JDBC> JDBC Resources
      2. Right click the database you used in the project, and select Properties
      3. Write down the Pool Name
      4. Rght click Servers> GlassFish Server> Resources> JDBC> Connection Pools
      5. Right click the pool name you found and select properties
      6. Write down the properties
    2. Create a connection to the database in Netbeans
      1.  Right click the Databases tab and select New Connection...
      2. Select a Java DB (Network) driver and click Next>
      3. Enter the properties of the server database
      4. Append to the JDBC URL
         ;create=true
      5.  Click Finish
    3. Insert the entries
      1. Right click the database connection you created an select Connect...
      2. Right click the database connection you created an select Execute Command...
      3. Copy the contents of this file into the window
      4. Right click in the window and select Run File
  6. Test the application. Verify that all entries returned from the database are in CAPITALS.

No comments:

Post a Comment