This is an archive of an inactive wiki and cannot be modified.


This technique gives a possible implementation for the CategoryBpImagesResizing using JSR-188. The idea is to capture client's profile information (CC/PP or, more likely, UAProf) and serve the version of an image with the resolution that best fits the display of the client. The idea of the example shown below is a servlet which returns an (X)HTML document to the client referencing an image. There would be a repository of different versions of the same image and the img element in the (X)HTML document would point to the image which best fits the display of the client. The resolution of the display is known by checking CC/PP or UAProf information, as declared by the client via HTTP GET request. Most of desktop user agents do not provide CC/PP and/or UAProf information so the example should be tested at client side by a mobile device or an emulator.

An improved version of this example might be done with any transcoding java library (there are some free and/or open-source available) in order to dynamically create an image of desired resolution.


Implementation Steps:


import java.util.HashMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageAdaptation extends javax.servlet.http.HttpServlet implements
  // Object of class "ProfileParser" in charge of getting 
  // client's capabilities
  private ProfileParser profileParser;
  public ImageAdaptation()
  public void init(ServletConfig config)
    ServletContext context = null;
    // Get the context and set a "ProfileParser" object with such information
    context = config.getServletContext();
    profileParser = new ProfileParser(context);
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
    // 'String' object used to store the value of attribute 'ScreenSize'
    String bppVal;
    // 'HashMap' object used to get the set of attributes
    HashMap attrMap;
    // Array of 'String' object used to get dimensions in attribute 'ScreenSize'.
    String Dimensions[];
    // 'String' object used to express the image to be loaded.
    String W3CLogoImage;
    PrintWriter out = response.getWriter();
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" ");
    out.println("<html xmlns=\"\">");
    out.println("<title>Image Adaptation (CC/PP - UAProf)</title>");
    // Check whether client declares a CC/PP profile
    // and show profile's file name
    if (request.getHeader("profile") == null)
      System.out.println("profile: <NULL>");
      System.out.println("profile: " + request.getHeader("profile"));
    // Same for UAProf 
    if (request.getHeader("x-wap-profile") == null)
      System.out.println("x-wap-profile: <NULL>");
      System.out.println("x-wap-profile: " + request.getHeader("x-wap-profile"));
    // Get hash table with attributes (capabilities of the client)
    attrMap = profileParser.getProfileAttr(request);
    // If the object is null (attributes could not be read for some reason) 
    // then serve a default image (120 pixels wide as for default delivery context)
    if (attrMap == null)
      out.println("<img src=\"images/w3clogo120.png\" alt=\"W3C Logo\" />");
      out.println("<p>NULL PROFILE</p>");
    // If information about client is available...
      // Get attribute "ScreenSize"
      bppVal = (String) attrMap.get("ScreenSize");
      // Get each dimension of screen size
      Dimensions = new String[2];
      Dimensions = bppVal.split("x");
      // Create file name of the image to be served 
      // (file name expresses width of the image)
      W3CLogoImage = "w3clogo" + Dimensions[0] + ".png";
      // Insert the image into (X)HTML document      
      out.println("<img src=\"images/" + W3CLogoImage + "\" alt=\"W3C Logo\" />");
      out.println("<p>Profile exists.</p>");
      out.println("<p>ScreenSize: " + bppVal + "</p>");

Back to BestPracticesList

CategoryJava CategoryXhtml CategoryContentAdaptation CategoryBpImagesResizing CategoryBpCapabilities

Contributions to this wiki are governed by the W3C policies for Contribution to W3C' wiki on Mobile Web.