The skin server
Listing 5 contains the core of a basic skin server. The skin server
loads a property file that contains mappings of pairs (skin, ID) to
files. Each mapping is defined as <skin>.<id>=<file>. The skin server
then maps the request parameters to a file name and forwards the
request to that file. That file is finally transmitted to the receiver;
in this example, the file forwards to the browser that displays the
HTML for PersonalHomepage.jsp.
Listing 5: SkinServer.jsp
<%@ page language="Java" import="java.util.*, UserProfileBean" %>
<%-- Load Property bundle --%>
<%
Properties props = new Properties();
try {
props.load( new FileInputStream( application.getRealPath
("data/skin.properties") ) ) ;
}
catch( Exception e ) {
e.printStackTrace( System.err ) ;
}
String parameter = request.getParameter( "skin" ) + "." +
request.getParameter( "id" ) ;
%>
<jsp:forward page="<%=props.getProperty( parameter )%>"/>
In Listing 6, you can see the structure of the properties stored in a
property file. For example, a request for the skin blue_ocean's logo
element would map to the file data/yourco_blue_ocean.gif.
Listing 6: skin.properties
# -- General Structure --#
# .=
#-- Red Whip Skin --#
red_whip.css=/data/red.css
red_whip.logo=/data/yourco_red_whip.gif
#-- Blue Ocean Skin --#
blue_ocean.css=/data/blue.css
blue_ocean.logo=/data/yourco_blue_ocean.gif
#-- Green Piece Skin --#
green_piece.css=/data/green.css
green_piece.logo=/data/yourco_green_piece.gif
Improve Performance
You can improve performance by loading the properties files only once,
when the SkinServer JSP loads. For example, you could wrap the
properties in a JavaBean, which loads the properties only once and then
resides in memory until the JSP unloads.
Performance and file caching are important issues for a scalable
Website with a large number of users. Another viable approach to
improving performance would be to cache the skin elements in memory and
return a character or binary stream for each request.
You should also note that the skin server may become a bottleneck in a
distributed Website. In that case, load balancing for a set of skin
servers could address the problem.
Customize your Website
Users appreciate it when they can personalize Websites or portals,
particularly those sites that they use often. By providing predefined
skins, you can give your users a choice regarding the look of your
Website without sacrificing its professional design. You start by
factoring out the key visual elements and creating different designs
for users.
In this article, you learned how users can enter their preferences in a
form and how to store user profiles in JavaBeans. You also learned how
to use the user profile to display a customized Website with a skin
server. You should now have a clear idea of how to add personalization
features to your Website. The solution is easy to implement and can
easily grow to store more user preferences.