Example: UPServletExample.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;


//Create a Userprofile using the new API

public class UPServlet_ReadWrite extends HttpServlet 
{

    public void doGet  HttpServletRequest(req, HttpServletResponse res)
       throws ServletException, IOException 
    {

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

       UserProfileManager manager = UserProfileManager.getUserProfileManager();
       UserProfile userprofile;

       try 
       {
          //Try creating the UserProfile
          userprofile = manager.addUserProfile("mpareene");
       } 
       catch UserProfileCreateException(e1) 
       {

         try 
         {

           //Try finding the existing in readWrite mode.
           //Second argument indicates whether we want to get userprofile
           //in read only mode or read write mode.

           userprofile = manager.getUserProfile("mpareene",true);

         } 
         catch UserProfileFinderException(e) 
         {
            e.printStackTrace();
            return;
         }

       }


       //Set the properties

       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");

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

       //Checking whether it updated the info

       try 
       {

          //Getting the existing userprofile in ReadOnly mode.

          userprofile = manager.getUserProfile("mpareene",false);

       } 
       catch UserProfileFinderException(e1) 
       {

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


       //Displaying the properties of userprofile
 
       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>");

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

 

See Also

Managing user profiles
User profile development options