In this section I will develop a simple dynamic Web application based on the Model View Controller (MVC) design pattern, which will generate prime numbers for its clients. An MVC design pattern divides an application into the following components:


1. Model: The content or the actual data (or logic)

2. View: The presentation of data to the clients

3. Controller: It controls the application-for example, calling the model for business logic and then forwarding the request to 'view' fgr its presentation.

The Web application will be based on servlets and]SP. In very simple terms, servlets are server side applications that can dynamically extend the functionality of a Web server. and ]SP is a presentation technology based on servlets. If you w~nt to know more details about servlets and ]SP then ICl recommend doing a Google search for the answers.

Typical Web application development on an app server requires a directory structure like in Figure 3. Here. Approot is the root directory for our Web application. It contains the default page that will be served by the server to its clients.

In addition, it contains a special directory named WEBcINF This is the directory where our Web application-specific files will be stored. WEB-INF contains the following files and folders:

• The deployment descriptor file named web.xml.

• A classes folder that contains the model and controller classes of our MVC Web application

• A lib folder that contains third-party jar files

• A jsp folder that is]RUN specific; it will not be used in our sample application.
For our prime number generator Web application. create a directory structure as shown below:

• The top-level directory Approot contains the default page index.html. the img directory (which contains the images used in index.htm0. result.jsp (view of our MVC Web application). and the WEB-INF directory.

• WEB-INFcontains classes and the web.xmlfile (a deployment descriptor file used by the server to map servlet names to its classes). There is no need of lib and jsp directories. as we are not using third-party jar files or jrun. The default page for the prime number generating Web application. index.html.looks like what's shown below:

This index.html is presented to clients who access the Web application. As you can see in the above snippet. the method is GET and the action servlet is Prime Searcher.do. PrimeSearcher. do is just a name given to our controller servlet and .do is just a convention. The GET method indicates that whenever a client clicks the GENERATE PRiME button, the doGet method of the servlet is called.

Name:  Developing a simple Web application.jpg
Views: 81
Size:  24.3 KB

The deployment descriptor file. web.xmllooks like what's given below: The deployment descriptor file web_xml is specific to a Web application. and the app server uses it to map servlet names to its classes. Each Web application has its own web.xml file. It maps the servlet names to servlet classes-for example. in our application. PrimeSearcher.do is mapped to PrimeSearcher. and PrimeSearcher is mapped to Controller class.

The 'view' part of our Web application is a]SP page (resultjsp) that generates output for its client. It looks like the following: This resultjsp is internally translated into a servlet by the container. The ]SP gets the attribute named primes attached to the HttpRequest object by the Controller. This primes attribute is an iterator that contains the prime numbers generated by the model class.

The Controller code is shown below: It is a simple servlet class that extends HttpServlet. It takes the help of the Model class to generate the prime numbers. This Model class contains the actual logic to generate primes. The code looks like this:

This model class contains the actual logic to generate primes. and returns a list of generated prime numbers to the Controller.

To understand the entireiunctionality. assume that when a client requests our Web application to generate primes. then the request will be transferred to the controller class that gets the number entered by the client from the HttpRequest object. This number is passed to a model class. which actually generates prime numbers and has the real logic of our application. The model class returns a list that contains prime numbers. The Controller class gets the iterator of this list and attaches this iterator as an attribute to the HttpRequest object. and forwards the remaining functionality to the 'view' of our application, which is a JSP file. 'View' gets this iterator object and generates a page containing the generated prime numbers.