Web programming

Units WEB1P and WEB2P

Web programming exercises

It is crucial to your development as a web programmer that you actually write some web programs! The following are a selection of graduated exercises to guide your practice. You don't have to do all of them (though the more you do the better). You don't have to write the precise program described here - any one with similar functionality will do. If you want to progressively develop your own webapp then by all means do so.

Timeframe Exercise You could also do the exercises from Basham chapters

Session 0

Before the unit starts / in the very early stages of it

  1. Write and run any Java program. The aim is to familiarise yourself with the integrated development environment (NetBeans).
  2. You should also familiarise yourself with the debugging features of NetBeans.
1

Sessions 1-3

While learning about servlets

The aim is to familiarise yourself with writing and running simple servlets.

  1. Write a servlet similar to the HelloYou.java one (in webapp1) that adds a line of text to a file (called "log.txt") each time it is run. The line should record the date and time together with the value of the "name" parameter.
  2. Write a second servlet (called ShowLog) in the same webapp that displays the contents of the "log.txt" file to the user.

If you want to substitute your own application for the one suggested here, that would be fine.

Hints

2, 3, 4

Sessions 2-4

While learning about servlets

The aim is familiarise yourself with MVC and constructing web applications in an MVC style.

Modify each servlet you wrote above so that instead of displaying its output itself, it calls a JSP to do it for it.

To help you do this, look at the showBanana servlet (mvc.fruit.showBanana) in webapp1. Some notes to help you with this can be found here.

Hint: Pass the data read from the log file as a List of objects from the servlet to the JSP.

5, 6, 7, 8

Develop a web application (or extend an existing one) so that it stores information in a session variable. Use the session variable in the JSP output page.

6, 11
Develop a web application (or extend an existing one) so that it uses a filter. 13
Develop a web application that implements user authentication to restrict access to part of it. Use a filter to check access to all restricted parts. Have the filter redirect to a login page if the user in not logged in. Once a user has logged in successfully, record their identity in a session variable that the filter checks to see whether the user is logged in or not. Initially, either hard-code the users/passwords into your code or read them from a file. Later, convert your application to look up users and passwords in a database. 12

Sessions 7-8

While learning about JDBC and databases

Develop a web application (or extend an existing one) that connects to a database. You could use either MySql (if you have it installed), or use the built-in Java database.

The program could collect some simple data from the user (via a form) and store it in a database table. Using JPA/Hibernate is the easiest way to do this (since JPA will do all the JDBC calls for you).

n/a
If you want to get more sophisticated and do something more akin to a real-world application, try storing entities of more than one class in the database. Determine before you start whether the relationship between your entities is one-to-one, one-to-many, many-to-one, or many-to-many, and place attributes in your entity classes to indicate this. n/a

A separate series of exercises relating to JavaScript (sessions 5 and 6) can be found here.

 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The web programming units include some material that was formerly part of the WPRMP, WECPP, WPSSM and WEMAM units.