Listing 2: UserProfileBean.java
// UserProfileBean.java
public class UserProfileBean {
String userId ="Anonymous";
String preferredSkin ="Default" ;
public void setUserId( String id ) {
userId = id ;
}
public String getUserId() {
return userId ;
}
public void setPreferredSkin( String skinName ) {
preferredSkin = skinName ;
}
public String getPreferredSkin() {
return preferredSkin ;
}
} ;
Process the user profile
The form containing the user preferences is submitted to
ProcessUserProfile.jsp. Listing 3 shows a sample JSP that you can use
to process the user profile information. It does not do anything
practical, but you can use it as a starting point for storing the user
profile in an LDAP (lightweight directory access protocol) directory or
in a database.
Listing 3: ProcessUserProfile.jsp
<%@ page contentType="text/html" %>
<%@ page language="Java" %>
<%@ page import="java.util.*, UserProfileBean" %>
<jsp:useBean id="userProfile" class="UserProfileBean"
scope="session" />
<jsp:setProperty name="userProfile" property="*"/>
User ID is: <jsp:getProperty name="userProfile"
property="userId"/> <BR>
Preferred Skin : <jsp:getProperty name="userProfile"
property="preferredSkin"/>
<jsp:forward page="/jsp/PersonalHomepage.jsp"/>
ProcessUserProfile.jsp ultimately forwards the request to a different
JSP that makes use of the user profile.
Add personalization to your Website
You have seen how a user can choose a skin and how this information is
stored in a JavaBean. Next, you will learn how to use these preferences
to display a personalized Website.
Changes to your Website
After the user profile is saved, your JSPs can exploit it to
personalize a portal or homepage. Using the user profile bean in your
JSPs, you can easily access the relevant properties to generate a
personalized page. Listing 4 demonstrates how to use the user profile
to create a customized look and feel for the user's preferred skin.
Listing 4: PersonalHomepage.jsp
<%@ page contentType="text/html" language="Java"
import="UserProfileBean" %>
<JSP:USEBEAN id="userProfile" class="UserProfileBean" scope="session"/>
<JSP:SETPROPERTY name="userProfile" property="*"/>
<HEAD>
<TITLE>My Personalized Homepage</TITLE>
<!-- ATN: The following line has been wrapped for better legibility.
It should be a single line with no whitespace after "?skin=".
-->
<LINK rel="stylesheet"
href="http://localhost:8080/skins/jsp/SkinServer.jsp?skin=
<jsp:getProperty name="userprofile"
property="preferredskin"/>&id=css">
</HEAD>
<BODY>
<!-- ATN: The following line has been wrapped for better legibility.
It should be a single line with no whitespace after "?skin="
-->
<IMG src="http://localhost:8080/skins/jsp/SkinServer.jsp?skin=
<jsp:getProperty name="userprofile" property="preferredskin"/>&
id=logo">
<H1>My personalized Homepage!</H1>
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
<TR>
<TD>
<TABLE class="Portlet" width="100%" border="0" cellspacing="0"
cellpadding="0">
<TR>
<TD class="TitleBar">
Title Bar
</TD>
</TR>
<TR>
<TD class="ContentArea">
Ipsem lorem retequiem latus gratis forticicum adventiris
seculem.<BR>
Lorventic marbot simplif forticicum lorentus practicum sempre
isbit
</TD>
</TR>
</TABLE>
</TD>
<TD>
</TD>
<TD>
<TABLE class="Portlet" width="100%" border="0" cellspacing="0"
cellpadding="0">
<TR>
<TD class="TitleBar">
Title Bar
</TD>
</TR>
<TR>
<TD class="ContentArea">
Ipsem lorem retequiem latus gratis forticicum adventiris
seculem.<BR>Lorventic marbot simplif forticicum lorentus
practicum sempre isbit
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</BODY>
PersonalHomepage.jsp makes use of the preferredSkin property for a user
profile bean; the <LINK> and <IMG> tags now refer to a JSP's URL and
not to files. The following code demonstrates how to write an <IMG> tag
for a logo. <IMG SRC = "http://localhost:8080/skins/jsp/SkinServer.jsp?
skin=<jsp:getProperty name="userProfile"
property="preferredSkin"/>&id=logo"> The <IMG> tag represents the logo
for the personalized homepage. Instead of referring to a static file in
the SRC attribute, the tag now makes a request to a skin server. The
request contains two parameters: the preferredSkin property's value of
the userProfile bean and the visual element's identifier. With the
pattern demonstrated in Listing 4, you can replace references to static
files to make the skins exchangeable.
Next Week: Personalize your Web site, Part 3