Example: UPServletExampleExtended.java

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.ibm.websphere.userprofile.UserProfile;
import com.ibm.websphere.userprofile.UserProfileManager;
import com.ibm.websphere.userprofile.UserProfileCreateException;
import com.ibm.websphere.userprofile.UserProfileFinderException;
import com.ibm.websphere.userprofile.UserProfileRemoveException;
import com.ibm.servlet.personalization.userprofile.UserProfileExtendedSample;



public class UPServletExtendedSample extends HttpServlet 
{

    public void doGet  HttpServletRequest(req, HttpServletResponse res)
    throws ServletException, IOException
    {
        UserProfileManager manager = UserProfileManager.getUserProfileManager();
        UserProfile userprofile;

        PrintWriter out;

        res.setContentType("text/html");
        out = res.getWriter();


        try 
        {

            //try Creating the UserProfile

            userprofile = manager.addUserProfile("mpareene");

        } 
        catch( UserProfileCreateException e1) 
        {

            try 
            {  
                //try finding the existing in readWrite mode
                userprofile = manager.getUserProfile("mpareene",true);
            } 
            catch UserProfileFinderException(e) 
            {
                e.printStackTrace();
                return;
            }


        }


        userprofile.setAddress1("myaddress1");
        userprofile.setAddress2("myaddress2");
        userprofile.setFirstName("Pareene");
        userprofile.setSurName("Mike");
        userprofile.setDayPhone("555-6677");
        userprofile.setNightPhone("556-6765");
        userprofile.setCity("MYCITY");
        userprofile.setNation("myCountry");
        userprofile.setEmployer("MyEmployer");
        userprofile.setFax("7823470");
        userprofile.setLanguage("mylanguage");
        userprofile.setEmail("MyEmail@email");
        userprofile.setStateOrProvince("myState");
        userprofile.setPostalCode("xxxxx");

        //calling setCellPhone
        ((com.ibm.servlet.personalization.userprofile.UserProfileExtendedSample)
           userprofile).setCellPhone("346-4588");


        //Freeing resources held by userprofile
         manager.releaseResources(userprofile);
         userprofile=null;
        

        //Checking whether it updated the info

        try 
        {

            //Getting the existing userprofile
            userprofile = manager.getUserProfile("mpareene",false);

        } 
        catch( UserProfileFinderException e1) 
        {

            out.println("Error finding ");
            e1.printStackTrace();
            return;
        }



        out.println(userprofile.getAddress1()+"<br>");
        out.println(userprofile.getAddress2()+"<br>");;
        out.println(userprofile.getFirstName()+"<br>");;
        out.println(userprofile.getSurName()+"<br>");
        out.println(userprofile.getDayPhone()+"<br>");;
        out.println(userprofile.getNightPhone()+"<br>");;
        out.println(userprofile.getCity()+"<br>");
        out.println(userprofile.getNation()+"<br>");;
        out.println(userprofile.getEmployer()+"<br>");;
        out.println(userprofile.getFax()+"<br>");;
        out.println(userprofile.getLanguage()+"<br>");;
        out.println(userprofile.getEmail()+"<br>");;
        out.println(userprofile.getStateOrProvince()+"<br>");;
        out.println(userprofile.getPostalCode()+"<br>");


        //Calling getCellPhone
        out.println(((UserProfileExtendedSample)userprofile).getCellPhone()+"<br>");

        //Freeing resources held by userprofile
         manager.releaseResources(userprofile);
         userprofile=null;

        //For getting values by cellPhone
        out.println("<br><br>Retreiving by Cell Phone <br>");
        Enumeration enum = manager.findUserProfiles("cellPhone","346-4588");
        while(enum.hasMoreElements()) 
        {

            com.ibm.websphere.userprofile.UserProfile up = 
               (com.ibm.websphere.userprofile.UserProfile)enum.nextElement();
            out.println("first name :"+up.getFirstName()+"<br>");

            //Freeing resources held by userprofile
            manager.releaseResources(up); 
        } 
    } 
}

 

See Also

Managing user profiles
User profile development options