JSR 286 Exercise 3 Solution
CensusViewPortlet.java
... @Override public void serveResource(ResourceRequest request, ResourceResponse response) { String preferredStateImage = request.getParameter("preferredStateImage"); try { String filename = "/home/holdorph/projects/portal/apache-tomcat-6.0.32/webapps/CensusView-1.0/WEB-INF/images/" + preferredStateImage + ".jpg"; if ((preferredStateImage != null) && (preferredStateImage.length() > 0)) { response.setContentType(getPortletContext().getMimeType( filename)); OutputStream os = response.getPortletOutputStream(); byte b[] = new byte[1024]; InputStream is = new FileInputStream(filename); int numRead = 0; while ((numRead = is.read(b)) > 0) { os.write(b, 0, numRead); } os.flush(); } else { System.err.println("ERROR! No Preferred State Image detected."); } } catch (IOException e) { e.printStackTrace(); } } ...
view.jsp
... <%@taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %> ... <portlet:resourceURL var="imageURL"> <portlet:param name="preferredStateImage" value="${preferredState}"/> </portlet:resourceURL> ... <img border="0" src="${imageURL}" height="80" width="80" alt="state" style="float:right" /> ...