< Previous | Next >

 

Lesson 2: Create server side Java classes

In this lesson you will learn to create the server side Java™ classes that will hold the data for the application.

To create the server side Java classes:

  1. Create the web2 Java package:

    1. In the Enterprise Explorer view, expand

      web2Project. Right-click

      Java Resources and select

      New | Package. The

      New Java Package wizard opens.

    2. In the Name field, type web2, then click Finish. The web2 Java package is created:

    The web2 package will contain the server-side objects that will be exposed to the Dojo user interface that you will create in Lesson 4: Create the Dojo client. In order for Dojo to interact with the Java objects that you created, you will need to make their methods invokable through a URL.

  2. Create the Movie Java class:

    1. Right-click

      web2 and select

      New | Class. The

      New Java Class wizard opens.

    2. In the Name field, type Movie, then click Finish. Movie.java opens in the editor.

    3. Edit Movie.java, so that it looks like the following:

      package web2;
      
      public class Movie {
      	private String title;
      	private String director;
      	private String actor;
      	private int rating;
      	public String getTitle() {
      		return title;
      	}
      	public void setTitle(String title) {
      		this.title = title;
      	}
      	public String getDirector() {
      		return director;
      	}
      	public void setDirector(String director) {
      		this.director = director;
      	}
      	public String getActor() {
      		return actor;
      	}
      	public void setActor(String actor) {
      		this.actor = actor;
      	}
      	public int getRating() {
      		return rating;
      	}
      	public void setRating(int rating) {
      		this.rating = rating;
      	}
      
      }
    The Movie class is a simple Java bean with string fields for a title, director, actor, and an integer field for a movie rating.

  3. Create the MovieService Java class:

    1. Right-click

      web2 and select

      New | Class. The

      New Java Class wizard opens.

    2. In the Name field, type MovieService, then click Finish. MovieService.java opens in the editor.

    3. Edit MovieService.java, so that it looks like the following:

      package web2;
      
      import java.util.ArrayList;
      import java.util.List;
      
      public class MovieService {
      
      	public List<Movie> getMovieList() {
      		List<Movie> movieList = new ArrayList<Movie>();
      
      		Movie goneWithTheWind = new Movie();
      		goneWithTheWind.setTitle("Gone with the Wind");
      		goneWithTheWind.setDirector("Victor Fleming");
      		goneWithTheWind.setActor("Vivien Leigh");
      		goneWithTheWind.setRating(10);
      		movieList.add(goneWithTheWind);
      
      		Movie backToTheFuture = new Movie();
      		backToTheFuture.setTitle("Back To The Future");
      		backToTheFuture.setDirector("Robert Zemeckis");
      		backToTheFuture.setActor("Michael J Fox");
      		backToTheFuture.setRating(10);
      		movieList.add(backToTheFuture);
      
      		Movie starWars = new Movie();
      		starWars.setTitle("Star Wars");
      		starWars.setDirector("George Lucas");
      		starWars.setActor("Harrison Ford");
      		starWars.setRating(10);
      		movieList.add(starWars);
      
      		return movieList;
      	}
      
      	public Movie getMovie(String title) {
      			List<Movie> movieList = getMovieList();
      			for (Movie movie : movieList) {
      				if (title.equals(movie.getTitle())) {
      				return movie;
      				}
      			}
      			return null;
      		}
      	}
    The MovieService class contains two methods:
    getMovieList() This class returns a list of Movie objects.
    getMovie(String title) This class returns a Move that has the same name as the passed argument.

    In this tutorial, all of the data is hard coded into the class for the purpose of simplifying the tutorial. If you were to implement a Web application in production, the data would come from databases, EJBs, or other server-side components.

In the Enterprise Explorer view of the Web perspective, your web2Project Web project looks like the following:

 

Lesson checkpoint

You have created the server side Java classes that will hold the data for your application.

< Previous | Next >