title: How to access managed bean and session bean from Servlet
body: <p>Here is how my <code>commandLink</code> work</p>
  
  <pre><code> &lt;p:dataTable value="#{myBean.users}" var="item"&gt;
       &lt;p:column&gt;
           &lt;h:commandLink value="#{item.name}" action="#{myBean.setSelectedUser(item)}" /&gt;     
       &lt;/p:column&gt;
   &lt;/p:dataTable&gt;
  </code></pre>
  
  <p>then in <code>myBean.java</code></p>
  
  <pre><code> public String setSelectedUser(User user){
       this.selectedUser = user;
       return "Profile";
   }
  </code></pre>
  
  <p>Let assume the user name is <code>Peter</code>. Then if I click on <code>Peter</code>, I will set the <code>selectedUser</code> to be Peter's User Object, then redirect to the profile page, which now render information from <code>selectedUser</code>. I want to create that same effect only using <code>&lt;h:outputText&gt;</code>, so GET request come to mind. So I do this</p>
  
  <pre><code> &lt;h:outputText value="{myBean.text(item.name,item.id)}" /&gt;
  </code></pre>
  
  <p>then the <code>text(String name, Long id)</code> method just return </p>
  
  <pre><code>"&lt;a href=\"someURL?userId=\"" + id + "&gt;" + name + "&lt;/a&gt;"
  </code></pre>
  
  <p>all that left is creating a servlet, catch that <code>id</code>, query the database to get the <code>user</code> object, set to <code>selectedUser</code>, the redirect. 
  So here is my servlet</p>
  
  <pre><code>public class myServlet extends HttpServlet { 
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          Long userId = Long.parseLong(request.getParameter("userId"));
      }
  }
  </code></pre>
  
  <p>Now I have the <code>id</code>, how do I access my session bean to query the database for the <code>user</code>, then access managed bean to set the <code>user</code> to <code>selectedUser</code>, then redirect to <code>profile.jsf</code>?</p>