3 Replies Latest reply on Mar 24, 2010 3:44 PM by aj1m aj1m

    get a session seam variable from servlet

    aj1m aj1m Newbie

      Hi,
      I am trying to get a session seam variable from servlet


      @Name("mybean")
      @Scope(ScopeType.SESSION)
      
      public class Mybean{
        private String var;
        //var setter and getter
      }
      



      and the servlet



      public class Myservlet extends HttpServlet{
      protected void doGet(HttpServletRequest request, HttpServletResponse response){
      
      //here I want to set mybean.var 
      //something like that
      mybean.setVar("value");
      
      }
      protected void doGet(HttpServletRequest request, HttpServletResponse response){
      doGet(request,response);}
      


      I added to my servlet :


      @AutoCreate
      @Name("applicationparams")
      @Scope(ScopeType.SESSION)
      public class Myservlet extends HttpServlet
      {
      @In
      private Mybean mybean;
      



      and i tried to get the mybean.var
      but i get a nullpointerexception


      is there a way to get mybean.var from the servlet

        • 1. Re: get a session seam variable from servlet
          Emir Calabuch Newbie

          A servlet is instantiated by the container, not by Seam, and therefore, even if you annotate it as such, it is not a Seam component. Not being a component, it is not managed by Seam and cannot therefore benefit from Seam services such as bijection.


          In order to access a component from non-component code, you can use Seam API calls:


          ((Mybean)Component.getInstance("myBean")).setVar("value");
          



          Also, note that servlet requests are not contextual (so, you don't have Seam contexts associated with it, i.e., you don't have a conversation context). This is because the request is not processed by Seam, but by your own servlet, and therefore Seam does not have the information necessary to determine what contexts apply to your request.


          What this means is that, even if you access a component via the API, if it tries to inject other components into itself, those other components could be unavailable. To have access to the contexts of the normal Seam application, you can give Seam the chance to see the request and determine its context, doing the following in your servlet:


          protected void doGet(HttpServletRequest request, HttpServletResponse response) {
              new ContextualHttpServletReques(request) {
                  @Override
                  public void process() {
                      doRequest(request, response);
                  }
              }.run();
          }
          
          private void doRequest(final HttpServletRequest request, final HttpServletResponse response) {
              response.setContentType(...);
              // from here you can get hold of a component, having Seam contexts visible.
              ((MyBean)Component.getInstance("myBean")).setVar("value");
          }
          



          What this does is let Seam get hold of the request and process it as if it arrived to its own servlet. I haven't tried this on an external servlet, but it does work fine from the Resource Servlet.

          • 2. Re: get a session seam variable from servlet
            aj1m aj1m Newbie

            Here is the solution :


             HttpSession session = request.getSession(true);
            Mybean mybean = new Mybean();
            mybean = (Mybean)session.getAttribute("mybean");
            mybean.getvar();
            



            ;)

            • 3. Re: get a session seam variable from servlet
              aj1m aj1m Newbie

              and to set a value :




               HttpSession session = request.getSession(true);
              Mybean mybean = new Mybean();
              mybean = (Mybean)session.getAttribute("mybean");
              mybean.setvar();
              session.setAttribute("mybean", mybean);