1 Reply Latest reply on Mar 22, 2009 8:09 PM by Marcus Spiegel

    Set a Seamsession in a CustomServlet

    Steffen Persch Newbie

      Hi Together,


      i wrote a CustomServlet to handle uploads from a flashcomponent. Now i am using Componetnt.getInstance(...) to get my seam component which should manage this all. But with every call i get a new instance. I'm was looking for a solution the last 2 days and i despair. Before i forget it, i send my jsessionid to the servlet as a get parameter.


      Hope you can help me, here is my servletcode:



      package com.rollertreffen.service;
      
      import java.io.*;
      
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      import org.apache.commons.fileupload.*;
      import org.apache.commons.fileupload.servlet.*;
      import org.jboss.seam.*;
      
      public class UploadServlet extends HttpServlet {
          private static final long serialVersionUID = 1L;
      
          @Override
          protected void service(final HttpServletRequest req, HttpServletResponse res)
                  throws ServletException, IOException {
              req.setCharacterEncoding("UTF-8");
              
              getUploads(req);
      
              res.setContentType("application/json");
              PrintWriter out = res.getWriter();
              out.println("{empty}");
              out.close();
          }
          
          protected void getUploads(HttpServletRequest req) {
              final String sessionId = req.getParameter("jsessionid");
              System.out.println("Session ID: " + sessionId);
              final ServletFileUpload upload = new ServletFileUpload();
              try {
                  PictureManager pms = (PictureManager) Component.getInstance("pms", ScopeType.SESSION);
                  for (FileItemIterator it = upload.getItemIterator(req); it.hasNext();) {
                      final FileItemStream item = it.next();
                      System.out.println("Fieldname: " + item.getFieldName());
                      final byte[] uplBytes = getByteArray(item.openStream());
                      System.out.println("UPLOAD: " + uplBytes.length);
                      if ("form-demo:picture1".equals(item.getFieldName())) {
                          pms.addFile(uplBytes);
                      }
                  }
                  pms.upload();
              } catch (FileUploadException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
          
          protected byte[] getByteArray(InputStream is) throws IOException {
              final ByteArrayOutputStream bos = new ByteArrayOutputStream();
              final byte[] buffer = new byte[32768];
              for (int read; -1 != (read = is.read(buffer));) {
                  bos.write(buffer, 0, read);
              }
      
              is.close();
              bos.close();
      
              return bos.toByteArray();
          }
          
      }



      Best regards, Steffen

        • 1. Re: Set a Seamsession in a CustomServlet
          Marcus Spiegel Newbie

          Either do something like:



          new ContextualHttpServletRequest(req) {
             @Override
             public void process() throws Exception {
                PictureManager pms = (PictureManager)Component.getInstance("pms", ScopeType.SESSION);
             }
          }.run();





          or register servlet in the Seam webcontext filter by adding to components.xml:



          <web:context-filter url-pattern="<your-servlet-url" />
          




          See also documentation, chapter 29.1.4.8.