title: Accessing session variables outside servlet
body: <p>I'm working on a java webapp where I need to access records from database based on user login id.I set the login details in session variable after successful login. </p>
  
  <p>I want to do is something like this </p>
  
  <p>Select * from proj_recs where user_id= user_id (from session)</p>
  
  <p>Right now I'm passing username as a parameter, but I believe it's not a good practice. Is there a better way of accessing session variables outside servlet ? </p>
  
  <p><strong>Servlet</strong> </p>
  
  <pre><code>    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
  
          User user = (User) request.getSession().getAttribute("userInfo");
  
          System.out.println(user);
  
          if(user != null){
              Gson gson = new Gson();
              returnJsonResponse(response,gson.toJson(user));
              return;
          }
  }
  </code></pre>
  
  <p><strong>In data layer package</strong> </p>
  
  <pre><code>public Accrual getAccruals(String accrualID,String userid) throws AccrualNotFoundException{
  
      String sql = Select * from db_acc where acc_id= accrualID and user_id=userid;
  
  }
  </code></pre>
  
  <p>The problem is I have to modify all my methods with userid. Is there a way I can set user details into some static class and access the details where ever i want in the application without modifying the method signature? But I believe static classes are share between different user requests.</p>