Using a URL query parameter to add a Component Presentation to a JSP Web page

An example of how to add a single Component Presentation to a JSP Web page dynamically, using a query parameter string.

Source Page

This Page Template combines with a Page to produce a publishable JSP Web page that contains a link to the target Web page, with a query parameter string specifying the Component to add, and the Component Template to render it with.

<%@page import="com.tridion.linking.DynamicComponentLink, com.tridion.linking.Link"%>
<%!
  final static String PUBLICATION_URI="tcm:0-2-1";
  final static String COMPONENT_URI="tcm:2-18";
  final static String COMPONENT_TEMPLATE_URI="tcm:2-16-32";
  final static String PAGE_URI="tcm:2-31-64";
  // This PAGE_URI must point to DynamicComponentLinkDestination.jsp.
  final static String LINK_TEXT="This is a Dynamic component link!";
%>
<html>
  <head>
    <title>Dynamic Component Link Source Page</title>
  </head>
  <body>
    Dynamic Component Link to ComponentPresentation 
    (componentId=<%=COMPONENT_URI%>, 
     componentTemplateId=<%=COMPONENT_TEMPLATE_URI%>) 
     on page <%=PAGE_URI%> in publication <%=PUBLICATION_URI%>:
<%
  DynamicComponentLink dcl = new DynamicComponentLink(PUBLICATION_URI);
  Link link = dcl.getLink(PAGE_URI, COMPONENT_URI,
  COMPONENT_TEMPLATE_URI, "dummy", LINK_TEXT, true);
  out.println(link.toString());
%>      
  </body>
</html>

In this Page Template, the dcl object validates links to Dynamic Component Presentations on the Page by making sure that both the Page that can assemble Dynamic Component Presentations and the Dynamic Component Presentation itself are available in the Content Broker. When DynamicComponentLink resolves a Link, the following parameters are added to the query parameter string:

Component
The ID of the Component
ComponentTemplate
The ID of the Component Template

The resulting published Web page contains the following content:

<a href="/thepage.aspx?Component=18&ComponentTemplate=16">
  This is a Dynamic component link!
</a>

Target Page

This Page Template combines with a Page to produce a publishable Web page that takes two query string parameters, one specifying the Component, the other specifying the Component Template. Both are combined into a Component Presentation and added to the Web page at request time.

<%@page import="com.tridion.web.jsp.ComponentPresentationAssembler, com.tridion.web.jsp.JSPPage"%>
<%!
  // Constants:
  final static String PUBLICATION_URI="tcm:0-42-1";
  final static String PAGE_URI="tcm:2-3456-64";
%>
<%
  // Get parameters from Request:
  int componentId = Integer.parseInt(request.getParameter("ComponentId"));
  int componentTemplateId = Integer.parseInt(request.getParameter("ComponentTemplateId"));
%>
<html>
  <head>
    <title>Dynamic Component Link Destination Page</title>
  </head>
  <body>
  Assembled Component:
  <%
    JSPPage dcdPage = new JSPPage(pageContext, PAGE_URI);
    ComponentPresentationAssembler cpa = new
     ComponentPresentationAssembler(dcdPage);
    out.println(cpa.getContent(componentId, componentTemplateId));
  %>
  </body>
</html>

The resulting published Web page contains the following content (the content under "Assembled Component" depends on the Component and Component Template specified):

<html>
  <head>
    <title>Dynamic Component Link Destination Page</title>
  </head>
  <body>
    Assembled Component:
    <b>This is the content of the component presentation!</b>
  </body>
</html>