ART#110 - How to pass parameters to droplet from JSP?



There are a lot of situations wherein we want to pass parameters from JSP to a droplet.. process those parameters and return some results.. 
Since we generally want to separate our JSP stuff and logic.. we rarely use "response.getOutputStream.println()" method.. We generally use our JSP to display parameters which are returned by our droplet.. we will be discussing all of these details soon...

The droplet flow..

Since a droplet is invoked from a JSP, we can pass parameters to a droplet from the JSP itself using ATG's special <dsp:droplet> tags (we will see "how" in a moment..) 
Now what happens is..
1. The JSP invokes a droplet, and in the process of invocation, it passes parameter(s).
2. The droplet's service() method is called, where our custom code is written.
3. In the service() method, we write code to fetch the parameter(s) passed from JSP.
4. Next, we want to return some output from droplet. To do this, we set a parameter in the droplet using java code.
5. We "service" an output parameter (whats that? we will cover soon). That output parameter can be used to fetch parameter which is set-up in the droplet. (This seems confusing.. parameter is serviced.. and then it called... blah blah blah... dont worry.. this will be clear soon)
6. Now, when JSP's output parameter is serviced, it displays whatever is present inside the output parameter.

Below diagram shows the above steps:-


Passing Parameters to droplet from JSP

We have created a droplet Parameters Droplet at location <ATG_CONFIG_PATH>/atg/test/ (we will see the droplet code in a moment).





First, we invoke a droplet using <dsp:droplet tag.
Next, we pass a parameter "name" with value "learnoracleatg.blogspot.com".

Below these two statements, are the statements for OUTPUT of droplet. 
NOTE: A very important point to be noted is that <dsp:oparam denotes the DROPLET's OUTPUT PARAMETER. The parameters which we are setting (e.g. "name" and "upperCaseName" in our case) are request parameters. Although we use these request parameters in the droplet, they are not droplet "specific". They are for a particular request.

"OUTPUT" can also be replaced by any other name (yes, you can even name it "dog"). In this case, "dog" would become our droplet's output parameter which we have to "service" from the droplet (we will soon see how to "service" a droplet parameter). When we service a droplet's output parameter, the contents inside <dsp:oparam> tags are displayed for that droplet. Now, the content inside <dsp:oparam> may contain normal text OR tags to display a request parameter (probably set from the droplet's java code itself), OR any JSTL tags.
A <dsp:droplet> tag can also contain multiple <dsp:oparam> tags with different droplet's output parameters. Out of these droplet's output parameters, droplet can choose to service any one of the output parameter, based on the logic written in our droplet's java code.

In our case, we have a single (droplet's) output parameter "OUTPUT". In droplet's <dsp:oparam> tags, we have displayed a request parameter "upperCaseName" using <dsp:valueof> tag. (We would be discussing details on these tags in further articles).

Our Droplet Code

Let us see the below droplet code first:-




In our droplet, we override the service() method.
Next, we fetch the value of parameter "name" passed from droplet and store it inside "myName" variable. 
---  "myName" variable now contains the string "learnoracleatg.blogspot.com".
Next, we change the case of this string to uppercase, and re-store it in "myName" variable.
---  "myName" now contains the string "LEARNORACLEATG.BLOGSPOT.COM"
Now, we set a request parameter "upperCaseName" with the value of "myName" variable.
--- request parameter "upperCaseName" now contains "LEARNORACLEATG.BLOGSPOT.COM"
Next, we service the "OUTPUT" parameter using serviceParameter() method, so that whatever is the content inside <dsp:oparam name="OUTPUT"> tags, that content will be displayed  by JSP.
(As we saw earlier, we could also name our droplet's output parameter as "dog", and we could service it).

Since our <dsp:oparam> tags contain the <dsp:valueof> tag for displaying the value of "upperCaseName" parameter, our droplet should finally display "LEARNORACLEATG.BLOGSPOT.COM" in the JSP.

Below is the screenshot to show that:-



I know, you all must be very excited to create your own droplets now.. but behold..!
There are many out-of-the-box droplets to do some of the tasks.. so before you jump into creating new droplets, you might actually wanna have a look at some of the OOTB droplets.. as you would not want to create some new droplets which are already present in ATG.. 
let us see some OOTB droplets that ATG provides..

Back



Next






12 comments:

  1. Looks like typo on the first line:
    There are a lot of situations wherein we want to pass parameters from JSP to droplet.. process those parameters

    ReplyDelete
    Replies
    1. Hi Sudhakar,

      Thanks for pointing out.. It has been corrected now. :)

      Delete
  2. is funny the total of code you have to type, just for a print out a line in the browse, any way thx for tutorials.

    ReplyDelete
    Replies
    1. Hi,

      This is a beginners course, therefore, i try to leave very less room for questions, by posting exact examples :)

      Delete
  3. Hi Yusuf,
    Is it mandatory to service an output parameter. Can't we get the value of upercase parameter directly in jsp without using output

    ReplyDelete
    Replies
    1. Yes. It is mandatory. If you dont do this, your droplet will not know, where to take input from and where to produce output.

      Delete
  4. finally i understood this.
    thanks man.

    ReplyDelete
  5. how to "service" a droplet parameter???reply on this

    ReplyDelete
    Replies
    1. You can use the method request.serviceParameter() method to service a parameter.

      Delete

Subscribe

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!

Flickr