Droplets/ATG Servlet Beans are one of the most important out of the box components provided by ATG. Droplet is nothing but a Servlet, Droplets are mainly used to abstract out the business logic in server side code. Droplet helps the developer to separate the presentation code from the business logic code. You may write your own Droplet if the provided Droplets are not meeting your requirements.
Droplet Static Design
steps to write a custom droplet
1. Write a java class which extends the atg.servlet.DynamoServlet class. Write the custom business logic inside the service method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package com.ourownjava.atg; import java.io.IOException; import javax.servlet.ServletException; import atg.core.util.StringUtils; import atg.nucleus.naming.ParameterName; import atg.servlet.DynamoHttpServletRequest; import atg.servlet.DynamoHttpServletResponse; import atg.servlet.DynamoServlet; /** * @author ourownjava.com * @date June 9th, 2012 * * How to write a custom droplet in ATG * */ public class CalculatorDroplet extends DynamoServlet { private static final String OPERANDS = "operands"; private static final String OPERATOR = "operator"; private static final String ADD = "+"; private static final String SUB = "-"; private static final String MUL = "*"; private static final String OPERAND_DELIMITER = ","; private static final String SPACE = " "; private static final String EQUALS = "="; private static final String RESULT = "result"; private static final ParameterName OUTPUT = ParameterName .getParameterName("output"); @Override public void service(final DynamoHttpServletRequest request, final DynamoHttpServletResponse response) throws ServletException, IOException { final String operands = request.getParameter(OPERANDS); final String operator = request.getParameter(OPERATOR); if (StringUtils.isEmpty(operands) || StringUtils.isEmpty(operator)) { throw new IllegalArgumentException( "Operands & Operator can't be null or empty. Operator ->" + operator + " Operands ->" + operands); } final String[] tempOperands = operands.split(OPERAND_DELIMITER); if (tempOperands.length != 2) { throw new IllegalArgumentException( "CalculatorDroplet supports only two operands! operand delimiter is ',' "); } request.setParameter(RESULT, calculate(operator, tempOperands)); request.serviceLocalParameter(OUTPUT, request, response); } private String calculate(final String operator, final String... operands) { final StringBuilder builder = new StringBuilder(); builder.append(operands[0]); builder.append(SPACE); builder.append(operator); builder.append(SPACE); builder.append(operands[1]); builder.append(SPACE); builder.append(EQUALS); builder.append(SPACE); if (ADD.equalsIgnoreCase(operator)) { builder.append(Integer.valueOf(operands[0]) + Integer.valueOf(operands[1])); } else if (SUB.equals(operator)) { builder.append(Integer.valueOf(operands[0]) - Integer.valueOf(operands[1])); } else if (MUL.equalsIgnoreCase(operator)) { builder.append(Integer.valueOf(operands[0]) * Integer.valueOf(operands[1])); } else { throw new IllegalArgumentException( "CalculatorDroplet doesn't support the given operator ->" + operator); } return builder.toString(); } } |
2. Write a property file to expose your Droplet as a component in ATG Nucleus.
1 2 3 4 |
#CalculatorDroplet.properties #/com/ourownjava/atg/CalculatorDropelt $class=com.ourownjava.atg.CalculatorDroplet $scope=request |
3. Place the CalculatorDroplet.properties file in the correct configuration location. This would register the CalculatorDroplet as component in ATG Nucleus.
4. Use the CalculatroDroplet in the jsp using dsp:droplet tag library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<%@taglib prefix="dsp" uri="http://www.atg.com/taglibs/daf/dspjspTaglib1_0"%> <dsp:importbean bean="/com/ourownjava/atg/CalculatorDroplet" /> <dsp:page> <div id="calculator"> <dsp:droplet name="CalculatorDroplet"> <dsp:param value="+" name="operator" /> <dsp:param value="12,7" name="operands" /> <dsp:oparam name="output"> <dsp:valueof param="result" /> </dsp:oparam> </dsp:droplet> <br/> <dsp:droplet name="CalculatorDroplet"> <dsp:param value="-" name="operator" /> <dsp:param value="12,7" name="operands" /> <dsp:oparam name="output"> <dsp:valueof param="result" /> </dsp:oparam> </dsp:droplet> <br/> <dsp:droplet name="CalculatorDroplet"> <dsp:param value="*" name="operator" /> <dsp:param value="12,7" name="operands" /> <dsp:oparam name="output"> <dsp:valueof param="result" /> </dsp:oparam> </dsp:droplet> </div> </dsp:page> |
5. Load the calculator.jsp in browser.
Your custom Droplet is up and running now!
Pingback: ATG Targeting Servlets/Droplets. | Java Tutorials
Pingback: ATG Droplet Static Design. | Java Tutorials
Pingback: How to write custom droplet in ATG? | Clean Java
Neat Explanation..Helpful article
Thanks Avin.
Pingback: ATG Droplet Static Design. | ATG
Usefull and direct info for ATG begginners. Thank you a lot and hope next post will be as good as this one.
Thanks Vico.
Pingback: ATG Targeting Servlets/Droplets. | ATG
Great work..Keep posting
Thanks Chakravarthy.
helpful article for the starters as well as experts , neat explanation.. can you provide an example for the formHandler also.
nice explanation