Skip to content

Instantly share code, notes, and snippets.

@martinlau
Created August 18, 2012 11:14
Show Gist options
  • Save martinlau/3386173 to your computer and use it in GitHub Desktop.
Save martinlau/3386173 to your computer and use it in GitHub Desktop.
A simple java portlet
package au.com.permeance.clojure;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import java.io.IOException;
public class JavaPortlet implements Portlet {
private PortletConfig config;
private int count;
public JavaPortlet() {
count = 0;
}
@Override
public void init(PortletConfig config) throws PortletException {
this.config = config;
}
@Override
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
String action = request.getParameter(ActionRequest.ACTION_NAME);
if ("increment".equals(action)) {
processIncrementAction(request, response);
}
else if ("reset".equals(action)) {
processResetAction(request, response);
}
}
@Override
public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {
if (PortletMode.VIEW == request.getPortletMode()) {
renderView(request, response);
}
else if (PortletMode.HELP == request.getPortletMode()) {
renderHelp(request, response);
}
}
@Override
public void destroy() {
config = null;
}
protected void processIncrementAction(ActionRequest request, ActionResponse response) {
count++;
}
protected void processResetAction(ActionRequest request, ActionResponse response) {
count = 0;
}
protected void renderHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException {
includePath("/help.jsp", request, response);
}
protected void renderView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
request.setAttribute("count", count);
includePath("/view.jsp", request, response);
}
protected void includePath(String path, RenderRequest request, RenderResponse response)
throws PortletException, IOException {
PortletContext portletContext = config.getPortletContext();
PortletRequestDispatcher requestDispatcher = portletContext.getRequestDispatcher(path);
requestDispatcher.include(request, response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment