HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
PROFESSIONAL JSP : ARCHITECTURE PART 2 - CODE FACTORING AND ROLE SEPARATION

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

One of the main reasons why the Java Server Pages technology has evolved into what it is today (and it's still evolving) is the overwhelming technical need to simplify application design by separating dynamic content from static template display data.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book Professional JSP.


The foundation for JSP was laid down with the initial development of the Java Web Server from Sun, which utilized page compilation and focused on embedding HTML inside Java code. As applications came to be based more on business objects and n-tier architectures, the focus changed to separating HTML from Java code, while still maintaining the integrity and flexibility the technology provided.

In Chapter 5 we saw how beans and objects can be bound to different contexts just by defining a certain scope. Good application design builds on this idea and tries to separate the objects, the presentation and the manipulation of the objects into distinct, distinguishable layers.

Another benefit of utilizing JSP is that it allows us to more cleanly separate the roles of a web production/HTML designer individual from a software developer. Remember that a common development scenario with Servlets was to embed the HTML presentation markup within the Java code of the Servlet itself, which can be troublesome. In our discussion, we will consider the Servlet solely as a container for Java code, while our entire HTML presentation template is encapsulated within a JSP source page. The question then arises as to how much Java code should remain embedded within our JSP source pages, and if it is taken out of the JSP source page, where should it reside?

Let's investigate this further. On any web-based project, multiple roles and responsibilities will exist. For example, an individual who designs HTML pages fulfills a web production role while someone who writes software in the Java programming language fulfills a software development role.

On small-scale projects these roles might be filled by the same individual, or two individuals working closely together. On a larger project, they will likely be filled by multiple individuals, who might not have overlapping skill sets, and are less productive if made too dependent on the workflow of the other.

If code that could be factored out to a mediating Servlet is included instead within HTML markup, then the potential exists for individuals in the software development role and those in the web production role to become more dependent than necessary on the progress and workflow of the other. Such dependencies may create a more error-prone environment, where inadvertent changes to code by other team members become more common.

This gives us some insight into one reason why we continue to develop basic Servlets: they are an appropriate container for our common Java code that has been factored out of our JSP pages, giving our software development team an area of focus that is as loosely coupled to our JSP pages as possible. Certainly, there will be a need for these same individuals to work with the JSP source pages, but the dependency is reduced, and these pages become the focus of the web-production team instead. Of course, if the same individual fulfills both roles, as is typical on a smaller project, such dependencies are not a major concern.

So, we should try to minimize the Java code that we include within our JSP page, in order to uphold this cleaner separation of developer roles. As we have discussed, some of this Java code is appropriately factored to a mediating Servlet. Code that is common to multiple requests, such as authentication, is a good candidate for a mediating Servlet. Such code is included in one place, the Servlet, instead of potentially being cut and pasted into multiple JSPs.

We will also want to remove much of our business logic and data access code from our JSP page and encapsulate it within JavaBeans, called worker or helper beans. We start to see a pattern of code movement from our JSP into two areas: a Servlet (or JSP) that sits in front of the main JSP, and JavaBeans that sit in back. We refer to this common pattern as 'Factor Forward-Factor Back', as shown in the figure below:

Another way to think about where code should be localized and encapsulated is that our JSP page should reveal as little as possible of our Java code implementation details.

Rather, the page should communicate our intent by revealing the delegating messages we send to worker beans, instructing them to get state from a model, or to complete some business processing.

Redirecting and Forwarding

Redirecting and forwarding requests in JSPs and Servlets takes place often, and it is important to understand the subtle difference between these two mechanisms even though they achieve the same goal (that is, a client asks for a resource on the server and a different resource is served to it):

  • When a Servlet or JSP resource chooses to redirect the client (using a response.sendRedirect(url)) the request object does not reach the second resource directly since the underlying implementation is an HTTP redirect. The server sends an HTTP 302 message back to the client telling it that the resource has moved to another URL, and that the client should access it there.

    The bottom line is that the lifecycle of the initial request object that was accessed in the first JSP terminates with the end of the service method in the first JSP, or with the reply from the server.
  • In a forward mechanism the request object is forwarded to the second resource, thus maintaining any object bindings to the request and its state, without a round trip to the client on the network.

    This allows the first JSP to do some work internally and then send information to the second JSP asking it to do its bit. (Servlets used a chaining mechanism to do this). See Chapter 5 to get a clearer picture of scope.

    JSPs and Servlets can utilize the forwarding mechanism to delegate tasks amongst themselves, in the process of separating dynamic and static content.

Now, let's investigate how we build these systems.




7 RELATED COURSES AVAILABLE
INTRODUCTION TO JAVA PROGRAMMING
The aims of this Java training courses is to understand the role that Java plays on the Internet; describe the be....
JAVA (V1.2): ADVANCED PROGRAMMING
This course teaches the reader to learn, understand and become familiar with the advanced features of Java progra....
INTRODUCTION TO JAVABEANS
To go from the fundamentals of JavaBeans programming to the threshold of Advanced level. Gaining in depth progr....
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
 
1 RELATED JOBS AVAILABLE
JAVA DEVELOPER MANCHESTER
Computer Futures Solutions are seeking a Senior Java Developer for their Manchester based client. You will be joi....
CONTACT US
Monday 6th October 2008  © COPYRIGHT 2008 - VISUALSOFT