Show TOC Start of Content Area

Background documentation Hints on Designing J2EE Web Applications  Locate the document in its SAP Library structure

Choosing Between Servlets and JSP Pages

Servlets and JSP pages have similar characteristics and functions; Web application developers can therefore use both technologies interchangeably. However, we can make several recommendations when it is better to use which technology.

You can use JSP pages to generate the HTML (or any other text-based markup language) that provides the layout of your Web pages. By separating the HTML code of the pure Java code, JSP technology gives you the opportunity of easily dividing the Web designing and Java programming tasks. This way, Web designers can build and maintain the professional design of your Web pages without any knowledge of Java.

Servlets are better suited to controlling functions such as managing the application flow, request dispatching and so on. These functions can also be implemented by other classes such as Java Beans, a tag library, or other Java classes.

J2EE Web Applications and Model-View-Controller (MVC) Design Patterns

When developing simple J2EE Web applications, you can implement all the functions regarding presentation content generation, application logic, requests dispatching in a single Web component. However, for complex business applications you can consider applying the MVC design patterns.

MVC Design Patterns

This graphic is explained in the accompanying text

The MVC design separates the presentation logic and the application flow control from the data persistence and business logic. You can take advantage of this by centralizing the control of your application, avoiding code duplication, and achieving a clear and comprehensible application design. Such an application is easy to maintain or extend in the future.

A typical MVC-type of J2EE Web application introduces a front controller servlet, which receives requests from the client and dispatches them to the application model. It also selects views based on the model and the application session state. As a single-point of entry to the application, it can be used to concentrate global functions, such as security, there.

The MVC view component is implemented as a JSP. This is appropriate since JSP pages are suitable for generating the text-based content produced by the MVC model. JSP technology also enables you to use templates that satisfy the common layout requirement of a business Web page.

The MVC model represents the business logic and the underlying business data. In a J2EE application, the model is implemented using the Enterprise JavaBeans technology. In some cases, JavaBeans located in the Web-tier of the application can be an alternative.

 

End of Content Area