In the previous tutorials that we’ve examined before, a RenderResponse writer is used mainly for writing the Portlets’ fragments into Portal page. This way of forming the Portlets’ contents isn’t used elegantly as it’s also doesn’t adhere the separation of concern (SoC) concept.

Combining the business code and UI fragments cohesively would lead us to code hard to maintain, hard to trace and less agility. As such, it’s important for you to make some sort of separation between your parts; UI and Business.

Portlet Servlet JSP

This tutorial will help you getting all of these concepts achieved as it’s would show you a full example of how can orchestrate between Portlets, JSPs and Servlets for making an employee registration mission so organized.

Registration form that we’re going to implement, is a full-fledged one, as you can fill in the all required information for the employee that you may want be registered. Information like employee identifier, employee name, employee job and employee salary the Portlet accepts and the database should retain.

Any professional application must provide the end users a confirmation messages either the process has finished successfully or something went wrongly.

In between, you will learn the all detailed fine-grained information that may help you achieve the integration between Portlets, JSPs and Servlets. Different integration principles like RequestDispatcher, Portlet Tag Library, handling exceptions and the way in which can leverage all of these principles to get everything done smoothly.

Portlet Tag Library

JavaServer Pages can use Portlet Tag Library to get access of Portlet functionality. Any JSP pages can import the Portlet Tag library that’s already provided by pluto-taglib dependency.

<defineObjects/>, <param/>, <actionURL/>, <renderURL/> and <namespace/>, all of these are provided Tags for being used by the JavaServer Pages.

The <defineObjects/> Tag, is used to define several objects from the calling Portlet’s request in the JSP page. Using of <defineObjects/> would define three different variables to be used from within your JSP scriptlets. These variables are portletConfig, renderRequest and renderResponse.

The <param> Tag, is used to send a parameters for the <actionURL/> and <renderURL/>. Every parameter must be a pair of name and value.

The <actionURL/> and <renderURL/> are used for generating an action URL for calling action phase and a render URL for calling render phase, respectively. Both of them has provided a var attribute that would be used later on in the form action or link’s target.

The <namespace/> is used for generating a unique name that would be used with the JavaScript code for making unique JavaScript variables, libraries or resources.

Portlet Request Dispatcher

Similar like Servlet, RequestDispacther is to dispatch the request into another Web resource. However, PortletRequestDispatcher is the object that you may got if you’ve called getRequestDispatcher() or getNamedDispatcher() against PortletContext object.

The difference here is that, the getRequestDispatcher() has used a path for locating a Web resource, while getNamedDispatcher() has used a defined name for locating a Web resource.

You may invoke include and forward upon your PortletRequestDispatcher. Using of forward will close the output stream after it has been invoked, whereas, the include method leaves the output stream open.

Employee Table Design

Before getting started implementing the registration form, let’s see the structure of the Table and its relevant columns as well as the SQL statement that lead you to create similar one into your database.

Employee-Table (1)

Employee POJO

As we’ve stated the Employee Table previously, it’s very easy for you to expect the Employee POJO.

As you’ve seen, Employee POJO is just a normal Java bean that’s contained for four different attributes with their gets and sets methods. id, name, job and salary are attributes that the user must provide for getting employee registered normally.

Design View

Mainly, vast amount of frameworks that were implemented had considered MVC (Model, View & Controller) as a base design for them. MVC is a design pattern, that’s aimed to make concern separation in order to allow develop components in a loosely coupled manner.

Being MVC is a design pattern, it’s applicable for you to design your Portlets to be MVC-compliance. JSPs can be used for viewing purposes, Portlets as a controllers and a simple POJO (Plain Old Java Object) can be your models.

Find below sequence diagram that shows you the way in which the registration process being handled and how the proposed parts are working collectively making this mission possible.

Here’s detailed explanation for the diagram listed above:

  • RegisterEmployee JSP page is the view that it would be used for gathering the information for employee being registered.
  • RegisterEmployeePortlet is the controller, it would be used for handling the business and dispatching into next coming view. We’ve added a RegisterEmployeeServlet as a complement component that’s aimed to handle the employee registration business.
  • EmployeeDAO and ConnectionUtility are just a components that are contained all relevant database code.

Employee Registration Views

We’ve defined different views for the employee registration purpose, as the process of registration may result in a success or failure status, we’ve added two additional views for the main one (registerEmployee.jsp). The success.jsp and failure.jsp should help the user getting a confirmation messages, to know whether the process has been completed successfully or it’s failed and what’s the reason of failure.

registerEmployee.jsp

Here’s detailed explanation for the code listed above:

  • The form of employee registration doesn’t contain any strange code except that’s related for <portlet:actionURL/> Tag. Mainly, action URL is used for generating a URL would call the prcoessAction() when it’s clicked.
  • We’ve used generated action URL to form the action of the whole form. Notice using of Scriptlets for accessing the generated URL.

success.jsp

Here’s detailed explanation for the code listed above:

  • We’ve used <renderURL/> to generate a render URL that’s used for invoking render phase. One parameter has been passed, it’s status which will be used for dispatching purpose.
  • Implicit objects like request, response that are received by the JSP and Servlet can be used but with a limitation as many of their methods either perform no operation or return null when used inside a Portlet container.

failure.jsp

Here’s detailed explanation for the code listed above:

  • We’ve used two different Portlet Tags, <renderURL/> and <defineObjects/>.
  • We never be able of accessing the implicit object renderRequest, if we don’t use the <defineObjects/> Portlet Tag.
  • Implicit objects like request, response that are received by the JSP and Servlet can be used but with a limitation as many of their methods either perform no operation or return null when used inside a Portlet container.

Employee Registration Portlet

As we’ve defined the Views that are needed for covering the all scenarios that you may face while employee registration. It’s time to define the controller, following below RegisterEmployeePortlet that would help you dispatching into required views.

Here’s detailed explanation for the code listed above:

  • RegisterEmployeePortlet overrode two methods from its GenericPortlet parent class.
  • The doView() method will determine which view it should be delegate for based on the status parameter.
  • The processAction() will handle the registration action as it should delegate the action into RegisterEmployeeServlet.
  • In case the RegisterEmployeeServlet has completed its work sucessfully, a status successparameter will be set as a render parameter for getting accessed by the render phase after then.
  • In case the RegisterEmployeeServlet has initiated an exception the catch block will handle it and put a status failed as a render parameter for getting accessed by the render phase after then.
  • JSPs are located using the getRequestDispatcher() while Servlet is located using the getNamedDispatcher(). We’ve passed the name of the Servlet that’s registered in the deployment descriptor as a parameter.

Employee Registration Servlet

RegisterEmployeeServlet is used to complete the registration process.

Here’s detailed explanation for the code listed above:

  • As the form that was initiated the action has used a POST method, the Servlet should provide an implementation for it.
  • The employee object has been created by extracting its properties from the request object.
  • It’s Using an EmployeeDAO for creating the employee.
  • It’s Handling the exception in case you’ve got one, and throw a new one for Portlet informing purpose.

EmployeeDAO & ConnectionUtility

We’ve implemented two different singleton classes for handling the employee’s database operations and acquiring the needed connections respectively.

It’s important to know that the username and password of the database are read from connection.properties file that’s defined in the resources.

Deployment Descriptor

Maven Build File

Employee Registration Demo

It’s now time of executing the application that’s just developed. At the beginning, make sure that you’re deploying the application into your Pluto Portal and you get access to JournalDev page.

Once, you’ve got this page, be sure that the doView() method has been executed and it checks for status variable that’s already null. As a result, the doView() method has dispatched the request into registerEmployee.jsp. Just fill in all the employee’s information.

Fill in all required information

By clicking on Register, your RegisterEmployeePortlet’s processAction() is executed and the RegisterEmployeeServlet will handle the whole registration process. As a result, the employee must be registered and the user must be confirmed about that.

Employee Is RegisteredEmployee Record Is SavedNow, you have the opportunity to get back into registration form by clicking on the Register Another link. This link is associated with the <renderURL/> as it’s passed a status value. What’s happened if you try to register the same employee with the same identifier. You’re mostly got an exception relevant for the database.

Employee-Registration-Form-1024x306

Summary

Using of Portlets with respect to JSP and Servlet is consider so attractive things as it contains a lot of details you must be aware of. We’re already handled that on behalf of you as you can download the source code to know how’s everything going on. Contribute us by commenting below and let us know if you have any concern or questions want to be answered.

By admin

Leave a Reply