Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. Lab Activity and Homework 6
  2. In this activity you will repeat the proper roles of components in a Model 2 Architecture by using the appropriate scopes for transmitting objects. As a side activity you will use ORM with JPA to access the database.
  3.  
  4. The activity consists of a web application for a watch shop in which the users can log in and browse watches depending on the country in which they were produced.
  5.  
  6. The site has the following structure:
  7.  
  8. Root:
  9.  
  10. Web Pages:
  11. index.jsp
  12. login.html
  13. searchWatches.jsp
  14. seeWatches.jsp
  15. servlets:
  16. watches
  17. login
  18. logout
  19. listeners
  20. initListener
  21. domain
  22. Watch
  23. Country
  24. Each watch has a name, a type and a country in which it was produced. Each country has a name and a code (RO, FR, NL...)
  25.  
  26. Task 1
  27.  
  28. In index.jsp you have at the top Hello <Your name> or Hello Guest if not logged in. Under you have links for login (to login.html) , logout (to logout), Search Watches (to searchWatches.jsp).
  29.  
  30. In login.html you have a single text input in which you enter you name and a submit button. The submit causes the user to be logged in with the introduced name. The name will be stored in the session context.
  31.  
  32. Logout will invalidate the session.
  33.  
  34. In searchWatches you will be shown a select input containing the list of the countries for which you have products and a submit button. The list of countries does not change often and it will be expensive to take it for each query from the database. So we will keep it in the servletContext as an attribute "COUNTRIES". The list of countries is stored in taken from the database and stored in the servletContext attribute when the application starts so this will be done in a ServletContextListener called initListener. searchWatches.jsp will retrieve the COUNTRIES attribute from the servletContext and populate the list of options. When submit button is pressed the request is handles by watches servlet.
  35.  
  36. watches servlet performs the role of the controller in a Model 2 architecture (handling the request, populating the model and forwarding the request to the view). In our case it will retrieve the country parameter from searchWatches.jsp. Next, it will search in the database (usually you would do this using a DAO) and it will retrieve the list of watches that were produced in that country. This list (the model) must be shown in a view component, in our case seeWatches.jsp. To send it to a view component, the servlet will add the list of watches to the request scope and will use a request dispatcher to forward the request to the view.
  37.  
  38. seeWatches.jsp displays the list of watches retrieved from the request scope and has a link to the index.jsp page.
  39.  
  40. Task 2
  41.  
  42. For this exercise you will use JPA to access the database, review the associated lecture. In this case the steps are the following:
  43.  
  44. -create a new database, jpaHomework
  45.  
  46. -create a Persistence Unit in you project. Take care that you select jpaHomework database, you set the Table Generation Strategy to Create and that you check Use Java Transaction API. To select the jpaHomework database click Data Source -> new, write a name for the new data source (for example jdbc/JpaHomework) and select the database connection from the list. This is available in Netbeans 7.4 version so there might be differences in other versions.
  47.  
  48. -create Watch and Country as Entities making sure you choose the correct relation between them.
  49.  
  50. In this moment the database tables are not created, you have to run the application for the first time with a query or persist for the tables to be generated.
  51.  
  52. -in initListener insert code and select Use Entity Manager. Use the instance for the entityManager (em) to create a query that returns all the countries.
  53.  
  54. -in watches servlet insert code and select Use Entity Manager. Use the instance for the entityManager (em) to create a query that returns all the watches with the country with the same name/code as the parameter received from searchWatches.jsp.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement