Passing objects to a servlet - session or as byte[]

Hi
I'm creating my first servlet and I want opinions on my design. I plan to sue my servlet to generate an image. So in my jsp page I have an image tag with the following
<img src="/Servlet/GenerateImage">But my servlet requires data in an ArrayList from my JSP page. How can I pass the ArrayList to my servlet? I've done some research and been looking aorund the forums, and I noticed people either convert the object to byte[] or pass the object using the session. I've tried doing the byte[] method but I don't know how to pass the byte[] to my servlet. I tried passing it as a parameter using a query string, get the parameter as a string, and convert it to byte[] again, but it didn't work. I can't seem to get this approach to work.
I'm worried that if I use the session, and a large number of people access my page which runs the servlet, my server will be bogged down.
What is the best approach in doing this? It'd be great if some sample code or links are supplied
Thanks for all the help!

My servlet requires data in an ArrayList from my
JSP page. How can I pass the ArrayList to my servlet?
I've done some research and been looking aorund the
forums, and I noticed people either convert the object
to byte[] or pass the object using the session. If the ArrayList is derived in the JSP and is required in the servlet, then saving it as a session attribute is the best bet. I guess it would be possible to send all the data from the ArrayList to the client in a cookie, but that seems messy and inefficient.
I'm worried that if I use the session, and a large
number of people access my page which runs the
servlet, my server will be bogged down.The session attribute only contains a reference to the ArrayList and so in itself does not cause a performance problem. If you're concerned that the ArrayList is very large, you could consider saving the info to a disk file. Alternatively, could the ArrayList be derived in the servlet instead of, or as well as, the JSP? Does the servlet need all the Arraylist entries, or just a subset? If a subset, save that as a session attribute and dereference the ArrayList in the JSP. Also, think about when the ArrayList reference can safely be removed from the session.
What is the best approach in doing this? It'd be great
if some sample code or links are suppliedJSP
<jsp:useBean id="myArrayList" class="java.util.ArrayList" scope="session"/>Servlet
HttpSession session = request.getSession();
ArrayList al = (ArrayList) session.getAttribute("myArrayList");
.

Similar Messages

  • Passing objects from two servlets residing in  two  servers

    Suppose i need to pass an object from one servlet to another servlet ,that are residing in two servers(Tomcat).How can i achieve this.
    Plz help me in this regard.
    Thanks in Advance......

    Serialize them to XML and send as POST parameter

  • Servlet Session Object

              I need to get the lastAccessTime of the servlet which I have deployed on Weblogic
              7.0.
              I have found that by using ServletSessionRuntimeMBean interface which has a method
              called getTimeLastAccessed, I can get the same.
              I am not able to get the same. Can any one sujject the way I have to go for this.
              

    Hello Linus,
    Actually, you would want to retrieve the necessary objects from your servlet context
    and then pass them along to your session EJBs. It's not necessary to pass the
    entire ServletContext object to your session bean. You should wrap the values
    in DTOs (Data Transfer Objects / Value Objects) and pass them along to your session
    beans. Also, it's not good to couple your EJBs with Servlets, as they may be eventually
    accessed from standalone clients in the future. For more information on this design
    pattern, please see the following link:
    http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
    Best regards,
    Ryan LeCompte
    [email protected]
    http://www.louisiana.edu/~rml7669
    "Linus Nikander" <[email protected]> wrote:
    Is it possible to accesss objects stored in a Servlet session context
    from
    an EJB ? If so, how ?
    //Linus Nikander - [email protected]

  • How do I pass an object to a Servlet?

    Hello,
    I have a fully initailized object called x.
    I need to invoke a servlet from an application and pass this object x to the
    servlet.
    This object contains all the necessary parameters which the servlet needs.
    How can I invoke a servlet from a class and pass this object?
    For Example:
    class SendObjToServlet
    try
    URL u = new URL("servlet_path");
    }catch(Exception e)
    Now how and where do I pass an object to this servlet??
    Please can any one respond?

    http://www.j-nine.com/pubs/applet2servlet/
    this website will give you the basic idea. It shows you how to do it with an applet the same idea will work for an applcation.
    lee

  • Passing Objects from Servlet to Servlet

    Hei,
    how do I pass an object from one servlet to another.. or back to the same again?
    I have my servlet and there I want
    first create an object, then pass that object for processing and manipulating and then pass it for output of the final result..
    How can I send and get the object in a servlet?
    Big thx,
    LoCal

    Hei,
    sorry.. maybe I gave to less informations. In fact it's only one servlet...
    I can't post the original code but something that makes it clear :)
    I wrote the whole project without using Servlet-Technique but normal Java-App. But I designed it so, that I only had to rewrite one class.. and least I thought so.. but now I stuck at this Object-passing problem.
    Sorry.. but I'm pretty new to Servlets
    Thank you very much in advance for your help.
    public class Logger extends HttpServlet {
      private Templates templ = new Templates();
      private Person pers;
      public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String uname = req.getParameter("user");
        String pwd = req.getParameter("pwd");
        String sts = req.getStatus
        PrintWriter out = res.getWriter();
        switch(sts) {
          case 1: out.println(templ.getXHTMLEditPers(pers));
          break;
          case 2: out.println(templ.getXHTMLViewPers(pers));
          break;
          case 3: out.println(templ.getXHTMLView(pers));
          break;
          default: out.println(tmpl.getXHTMLLogin());
      public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      doGet(req, res);     
    }

  • How to share objects between two ADF Faces servlet sessions

    I gather the application scope is one way to go for sharing data between sessions in an ADF application. Are there other more generic methods of sharing data between servlet sessions?
    For example, let's say we have a JDev 101.3 ADFBC/JSF app. (or JDev 11g for that matter). In this app we also register a servlet in web.xml. So two people log into this application and access this defined servlet, creating two instances of that servlet...I guess. Is there a way for those two instances/sessions to talk to each other? Pass data back and forth? Should I read about JMS? Or does application scope sound better? I guess a direct communication would be more tidy, than putting data on an application bulletin board, lilke application scope.
    Any thoughts?

    Hi,
    Only one instance of each servlet defined in web.xml gets created, not one per user. You're right about the sessions though, one per user is created. You're right on the other parts as well I think you could use application scope to do something like that, or the data base for that matter, depending on the use case.
    Regards,
    ~ Simon

  • Global object visible for all sessions in WebDynpro application

    Hello everybody,
    in a normal java web application you have the possibility
    to put an object into the servlet context.
    This object is visible for all sessions(e.g. some configurations or global settings for the
    web application).
    Now my question:
    Is there a possibility in a WebDynpro application to put such an object into a "global context" visible for all sessions?
    So I am looking for a container in WebDynpro which has
    the same behaviour like the servlet context in a web application.
    Thank you for your ideas an suggestions.
    Greetings
    Anton

    Hi Anton,
    To make an Variable Global ,you have define variable at various levels based on Usage.
    1)Ex: If you want to define Variable between two different Components . when A component is embedded into B Component then you have to define at Component Interface level and Mapping has to be Defined.
    2) If you want to make Variable to be used within the component among the Iviews ,then you can define at Component Controller or Custom Controller and Mapping has to be defined at Iview Level.
    3) If you want to use Variable between two Webdynpro Iviews then you have define the eventing mechanism to pass the values among the EP Webdynpro Iviews.
    Hope this answer helps you.
    Thanks
    Madhan

  • Yet Another "Passing Objects" Thread

    Hi All,
    Quick question about passing objects:
    I have three classes - ClassA, ClassB and ClassC. Lets say I create an object of ClassA in ClassB. Also, I would like to send the ClassA object from ClassB to ClassC to update its data. My question is, can I set the ClassA object in ClassC and modify it through ClassB? Confusing? I'll try to create an example below.
    ClassA:
    public class ClassA {
           // data
    } ClassB: creates and calls update methods for ClassA object
    public class ClassB {
           ClassA object = new ClassA();
           // print object
           ClassC ClassCFacade = new ClassC();
           ClassCFacade.setClassAObject(object);
           ClassCFacade.updateClassAObject(object);
           // print object
    }ClassC: updates object
    public class ClassC {
          // set object
          public void setClassAObject(ClassA object) {
                   // sets object
          // get object
          public ClassA getClassAObject() {
                  // returns object
          // modify object
          public updateClassAObject() {
                ClassA o = getClassAObject()
                // modify o
                setClassAObject(o)
    }So if objects are passed by reference, handle, or whatever you call it, essentially the above code should modify ClassA object created in ClassB, meaning the ClassA object should display it was modified after second print in ClassB. Although I've tried this implementation and the ClassA object is not modified after updateClassAObject method call from ClassB.
    Any input would be appreciated.
    Thanks,
    Bob

    OK. Here's snippets of actual code:
    public class ClassA implements SessionBean, ClassARemoteBusiness {
        // ClassA Attributes
        Integer ID;
        String name;
        // SET METHODS
        public void setClassAID(Integer ID) {
            this.ID = ID;
        public void setClassAName(String name) {
            this.name = name;
        // GET METHODS
        public Integer getClassAID() {
            return ID;
        public String getCLassAName() {
            return name;
    import classa.ClassA;
    import classc.ClassC;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class ClassB extends HttpServlet {
        private ClassA object;   
        private ClassC ClassCFacade;
        private ClassCFacade = lookupClassCBean();
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
              HttpSession session = request.getSession();           
              // display object values - displays default values
              out.println("ID: " +object.getClassAID());
              out.println("Name: " +object.getClassAName());
             // update object
             ClassCFacade.setClassAObject(object);
             ClassCFacade.updateClassAObjectID(someInteger);
             ClassCFacade.updateClassAObjectName(someName);
              // display object values - displays updated values
              out.println("ID: " +object.getClassAID());
              out.println("Name: " +object.getClassAName());
        // LOOKUP METHODS
        private web.ServiceLocator serviceLocator;
        private web.ServiceLocator getServiceLocator() {
            // service locator code
        private classc.ClassCRemote lookupClassCBean() {
            // lookup code       
    import classa.ClassA;
    public class ClassC implements SessionBean, ClassCRemoteBusiness {
        private ClassA o;
        // SET METHODS
        public void setClassAObject(ClassA object) {
            o = object;
        // UPDATE
        public void updateClassAObjectID(Integer ID) {
            o.setClassAID(ID);
        public void updateClassAObjectName(String name) {
            o.setClassAName(name);
    }

  • Servlet Session data being shared

    I have a bunch of servlets tha basically generate reports. The problem I'm having is that when two users run the same servlet at about the same time, one of the reports will be over written by the data of the other report. It almost seems that the users are sharing the same context.
    Is there any way to fix this or do you have ny suggestions, tips as to how to prevent this.
    Thnaks in advance for any help you may provide.

    I ran into a similar problem not to long ago with sessions. I had an instance or global reference to the session object in my servlet. When two or more people used the servlet at just the right time I would get problems where data was getting used across sessions. For example:
    public class FooServlet extends HttpServlet
         HttpSession session;
         doGet(HttpServletRequest req, HttpServletResponse res)
              session = req.getSession(false);
              doStuffWithSessionData();
         doStuffWithSessionData()
              String temp = session.getValue("temp");
              //do some stuff
    }So, person A would connect to the servlet, and their session would be retreived, and the instance reference would get set to their session. Person B would connect at about the same time, their session would get retreived and the instance reference would get set to theirs... right before the getValue("temp") on person A's session is called. So, whatever I was doing for person A would end up using the data out of person B's session. I like to call this a race condition.
    It is important to keep in mind that servlets are accessed by multiple threads concurrently, so you need to make sure your servlets are thread safe. Instance variables that get modified with each request are a very bad, non-thread safe thing and will cause odd behaviour like what you are describing. I speak from experience.
    I fixed the above by removing the instance reference and doing the following:
    public class FooServlet extends HttpServlet
         doGet(HttpServletRequest req, HttpServletResponse res)
                   doStuffWithSessionData(req);
         doStuffWithSessionData(HttpServletRequest req)
              HttpSession session = req.getSession(false);
              String temp = session.getValue("temp");
              //do some stuff
              //NOTE: this works most of the time. However, if the client
              //connects with more than one browser window using the same
              //session, I could run into some trouble here too.
    }That was a quick fix. However, what I really need to do is create a separate class to encapsulate all user session data and access the session and all data through public static synchronized methods on that class, passing it the request object when I do it. That way all data is stored in one object, and that object is used to do all session access in a thread-safe, synchronized manner.
    I hope this information helps you and is understandable. If you have any questions I will try to clarify. Making things thread-safe can be a daunting task.

  • Forwarding specific object to a servlet

    Hi!
    In my servlet, I want to 'attach' some data as an integer and a specific object I have created. The servletOne is located in a server, servletTwo in another one. I need to pass objects.
    I want to do something like that:
    int myInteger = 1;
    client myClient = new client();
    myClient.setName("carlos");
    request.getRequestDispatcher("servletTwo").forward(request, response);In this case, I want to passa an integer (myInteger) and client (myClient) object.
    How can I do that?
    Thanks in advanced,
    Carlos

    About cookies... I really prefer do it with session
    (if it is the only way to do that).Most sessions are implemented using Cookies behind the scenes. :)
    How can I share a session for 2 different context?I believe that is entirely up to the Servlet container you are using, as the servlet spec is silent on this issue (IIRC). Check your container documentation. But keep in mind that a Cookie might be the only feasable way to go.

  • Get the Web users Clipboard text in Servlet session

    We have a small web application � uses JSPs , Servlets and runs on Tomcat Server.
    We are trying to get the end web user�s ( browsers ) Clipboards copied text in our Servlets session.
    We know there is some AWT API to get the Clipboards copied data.
    java.awt.datatransfer.Clipboard clipboard = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
    // Get data from clipboard
    java.awt.datatransfer.Transferable clipData = clipboard.getContents(clipboard);
    But if we use this piece of code in our servlet session, we always get the Clipboard text, which is copied on the Server. Not at the users / browsers.
    So, we wonder are there any ways or similar to get the Users clipboard text in that users session.
    Any help would be great.

    Its the same old story.
    Java code is only running on the server.
    At the client end all you can work with is HTML and javascript.
    With Internet explorer you can access clipboard data:
    http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/clipboarddata.asp?frame=true
    Grab it with javascript, put it into a hidden field, and submit the page.
    That will pass it as a request parameter to your jsp/servlet, and you can get at it that way.

  • Hoe to pass object as a parameter thru webSerive

    I need to pass Object as a parameter to web service:
    example:
    public calss returnCodes{
    int ret_code;
    String ret_string;
    public retrunCodes(int ret_code, String ret_string){
    this.ret_code = ret_code;
    this.ret_string = ret_string;
    public int getret_code()
    return this.ret_code;
    public String getret_string {
    return this,ret_string
    and other repc holder class to hold returnCodes
    public class retrunCodesHolder implents javax.xml.rpc.Holder
    returnCode value;
    pubic returnCodesHolder(){
    public returnCodesHolder(returnCodes value) {
    this.value = value;
    In Webservice I use returnCodesHolder as parameter.
    WebService getReturnCodesWs I have
    returnCodes retCodes = new returnCodes(04,"Good return code)
    returnCodesHolder retholder = new returnCodesHolder(retCodes)
    return myString;
    When I am calling above webService in servlet I have code:
    org.test.mytest.returnCodesHolder retHolder = new org.test.mytest.returnCodesHolder();
    String result = port.getReturnCodesWs(retHolder);
    How to I access /get values of returnCodes class in my calling servlet?
    I am using Tomcat v 6.0

    I believe (although have never used, so if I'm wrong,
    correct me please!) you can do it with implicit
    dynamic cursors, such as:
    For rec in 'select col1, col2 from table 1 '||lstr
    loop
    Not really:
    michaels>  declare
       lstr   varchar2 (30) := ' where empno = 7788';
    begin
       for rec in 'select * from emp e ' || lstr
       loop
          null;
       end loop;
    end;
    Error at line 1
    ORA-06550: line 5, column 4:
    PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
       . ( * @ % & - + / at mod remainder rem .. <an exponent (**)>
       ||

  • How to pass data from one internal session to another

    Hi SAP Experts,
    How to pass data from one internal session to another and from One external session to another external session. I used import and export parmeter and SPA/GPA parameters. What is the other way to pass data?
    Please tel me urgently
    Thank you
    Basu

    Memory Structures of an ABAP Program
    In the Overview of the R/3 Basis System you have seen that each user can open up to six R/3 windows in a single SAPgui session. Each of these windows corresponds to a session on the application server with its own area of shared memory.
    The first application program that you start in a session opens an internal session within the main session. The internal session has a memory area that contains the ABAP program and its associated data. When the program calls external routines (methods, subroutines or function modules) their main program and working data are also loaded into the memory area of the internal session.
    Only one internal session is ever active. If the active application program calls a further application program, the system opens another internal session. Here, there are two possible cases: If the second program does not return control to the calling program when it has finished running, the called program replaces the calling program in the internal session. The contents of the memory of the calling program are deleted. If the second program does return control to the calling program when it has finished running, the session of the called program is not deleted. Instead, it becomes inactive, and its memory contents are placed on a stack.
    The memory area of each session contains an area called ABAP memory. ABAP memory is available to all internal sessions. ABAP programs can use the EXPORT and IMPORT statements to access it. Data within this area remains intact during a whole sequence of program calls. To pass data to a program which you are calling, the data needs to be placed in ABAP memory before the call is made. The internal session of the called program then replaces that of the calling program. The program called can then read from the ABAP memory. If control is then returned to the program which made the initial call, the same process operates in reverse.
    All ABAP programs can also access the SAP memory. This is a memory area to which all sessions within a SAPgui have access. You can use SAP memory either to pass data from one program to another within a session, or to pass data from one session to another. Application programs that use SAP memory must do so using SPA/GPA parameters (also known as SET/GET parameters). These parameters are often used to preassign values to input fields. You can set them individually for users, or globally according to the flow of an application program. SAP memory is the only connection between the different sessions within a SAPgui.
    The following diagram shows how an application program accesses the different areas within shared memory:
    In the diagram, an ABAP program is active in the second internal session of the first main session. It can access the memory of its own internal session, ABAP memory and SAP memory. The program in the first internal session has called the program which is currently active, and its own data is currently inactive on the stack. If the program currently active calls another program but will itself carry on once that program has finished running, the new program will be activated in a third internal session.
    Data Clusters in ABAP Memory
    You can store data clusters in ABAP memory. ABAP memory is a memory area within the internal session (roll area) of an ABAP program and any other program called from it using CALL TRANSACTION or SUBMIT.
    ABAP memory is independent of the ABAP program or program module from which it was generated. In other words, an object saved in ABAP memory can be read from any other ABAP program in the same call chain. ABAP memory is not the same as the cross-transaction global SAP memory. For further information, refer to Passing Data Between Programs.
    This allows you to pass data from one module to another over several levels of the program hierarchy. For example, you can pass data
    From an executable program (report) to another executable program called using SUBMIT.
    From a transaction to an executable program (report).
    Between dialog modules.
    From a program to a function module.
    and so on.
    The contents of the memory are released when you leave the transaction.
    To save data objects in ABAP memory, use the statement EXPORT TO MEMORY.
    Saving Data Objects in Memory
    To read data objects from memory, use the statement IMPORT FROM MEMORY.
    Reading Data Objects from Memory
    To delete data clusters from memory, use the statement FREE MEMORY.
    Deleting Data Clusters from Memory
    please read this which provide more idea about memory
    Message was edited by:
            sunil kumar

  • Servlet to pass parameter to another servlet

    Hi, I have a question. I have two servlets, servler A and servlet B. I wanna do this:
    1)servlet A to pass a parameter to servlet B. No redirect is required.
    2)servlet B to accept the parameter.
    How do I do that?
    Thanks

    1)servlet A to pass a parameter to servlet B. No redirect is required.
    2)servlet B to accept the parameter.I completely agree with what capitao suggested.
    As U said tht u need not require a redirect functionality
    U actually need to implement a proxy kind of mechanism in this case
    You may also go by using HttpURLConnection Object to retrive the info by passing few parameters.
    checkout an eq code down below
    HttpURLConnection server = (HttpURLConnection)(new URL("http://<hostname>:<port>/servletC")).openConnection();
    server.setRequestProperty("testParam","testParam");
    InputStreamReader isr = new InputStreamReader( server.getInputStream() );
    BufferedReader in = new BufferedReader( isr );
    response.setContentType( "text/html" );
    PrintWriter outStr = response.getWriter();
    String line = "";
    while((line = in.readLine()) != null) {
    outStr.println( line );
    }

  • Passing objects to a applet.

    hi there,
    i have a jsp and a applet. i need to pass an object from my jsp to the applet and the applet will present the results.
    how can i do it?? how do i pass objects to the applet??
    thanks

    you typically pass strings as applet parameters in the page.
    otherwise, you have your applet connect back to the server with a socket or URLConnection to a servlet, which can send serialized objects if that's what you need, or other data.

Maybe you are looking for

  • BADI or BAPI Getting Trigerred at the time of PO approval or Creation

    Hi Gurus,                Can Anyone of you just guide me by giving one or more BADIs name that are getting trigerred at the time of PO approval or before Ordered. I am in the nick of the project. Please help urgently. Points will be rewarded. Thanks

  • Free goods tick mark coming automatically in PO?

    Hi Gurus, While creating PO, free goods tick mark coming automatically in PO if i m entering net price.. I have checked material master as well as vendor master.Nowhere free good maintained in both. Then, why it is picking automatically in PO? Pls he

  • Flat file to idoc - mapping

    Gurus, I have a flat txt file of following format Doc#         Whse        Item          Matnr         date                       code 3256999     WH20     1001     861927     09/12/200713:24     9596     2 3256999     WH20     1002     861928     09

  • Problems opening PDF in Adobe Reader

    When I try to open a PDF using Adobe Reader the Adobe Reader page opens but not my PDF. The computer freezes for a few seconds and then the Sdobe Reader page shuts down. I have tried uninstalling and reinstalling with no sucess. If it makes any diffe

  • Remaining hard drive space more than actual drive capacity!

    I have a retina macbook pro, and installed mountain lion so not sure which forum to put this post in. I guess posting on Mountain Lion might be better. My drive says 548GB available on my finder window. I actually only have 170GB left based on my Abo