How can I make my code more efficient?

Hello Everyone,
I have a problem that is something like this:
Say you have a database and inside that database you have a table called 'books' which has only 3 columns: id, title, author.
Now let's say you have a servlet/jsp (servlet1) that expects a parameter called 'author' and it will display a list of all books by a certain author to the client. It will display this list such that each book is a link to another servlet/jsp (servlet 2)that accepts the book 'id' [primary key] as a parameter. Servlet 2 will display all the information about that book to the client.
Servlet1 works by querying the database and building a list of beans, one of which will contain the same data as servlet 2 is going to need to display the book information. But I don't know how to make so that servlet2 can see the bean used in servlet 1. Right now servlet2 gets the primary key as a parameter and queries the database again. This can't be the right way to being going about this. Can anyone offer me some insight as to a better way to approach this problem?
Thanks v. much.
Chris Latimer

put all the data you wish the 2nd servlet to recieve from the first servlet in a bean or object and pass it to the second. This works great with small amounts of data, but if you are going to have huge beans there are other ways of doing it.
If you have huge beans, then you may consider updating/inserting into a table in the database and then reading the data from the table into the 2nd servlet.
tnx,
Les

Similar Messages

  • How can I make my code more optimized?

    This is the code for my android game I am trying to make:
    import flash.events.MouseEvent;
    import flash.media.SoundChannel;
    import flash.display.MovieClip;
    var STATE_INIT_GAME:String = "STATE_INIT_GAME";
    var STATE_PLAY_GAME:String = "STATE_PLAY_GAME";
    var STATE_END_GAME:String = "STATE_END_GAME";
    var gameState:String;
    var hearts:Array;
    var atoms:Array;
    var bombs:Array;
    var enemies:Array;
    var level:Number;
    var score:Number;
    var lives:Number;
    var tempHeart:MovieClip;
    var tempBomb:MovieClip;
    var tempAtom:MovieClip;
    var tempEnemy:MovieClip;
    var rSound:rMusic = new rMusic  ;
    var Rchannel:SoundChannel;
    var offset:int = 20;
    var enemyBaseSpeed:int = 4;
    var speedLevelInc:Number = 3;
    var MAX_SPEED:Number = 100;
    score = 0;
    roachLevel.score_txt.text = String(score);
    function gameLoopR(e:Event):void
        switch (gameState)
            case STATE_INIT_GAME :
                initGame();
                break;
            case STATE_PLAY_GAME :
                playGame();
                break;
            case STATE_END_GAME :
                endGame();
                break;
    function initGame():void
        Rchannel = rSound.play(0,9999);
        level = 1;
        roachLevel.level_txt.text = String(level);
        lives = 3;
        roachLevel.lives_txt.text = String(lives);
        hearts = new Array();
        bombs = new Array();
        atoms = new Array();
        enemies = new Array();
        gameState = STATE_PLAY_GAME;
    function playGame():void
        makeEnemies();
        moveEnemies();
        makeHearts();
        moveHearts();
        makeBombs();
        moveBombs();
        makeAtoms();
        moveAtoms();
        testForEnd();
    function makeBombs():void
        var chance:Number = Math.floor(Math.random() * 6000);
        if (chance <=  +  level)
            tempBomb.scaleX = 1.5;
            tempBomb.scaleY = 1.5;
            tempBomb = new Bomb();
            tempBomb.x = Math.round(Math.random() * 480);
            tempBomb.cacheAsBitmap = true;
            addChild(tempBomb)
            bombs.push(tempBomb);
            tempBomb.speed = 1;
    function moveBombs():void
        var tempBomb:MovieClip;
        for (var h:int =bombs.length-1; h>=0; h--)
            tempBomb = bombs[h];
            if (tempBomb.dead)
                Rchannel.stop();
                lives = 0;
                roachLevel.level_txt.text = String(lives);
                bombs.splice(h,1);
            else
                tempBomb.rotation += (Math.round(Math.random()*.4));
                tempBomb.y +=  (Math.cos((Math.PI/180)*tempBomb.rotation))*tempBomb.speed;
                if (tempBomb.x < 10)
                    tempBomb.x = 11;
                if (tempBomb.x > stage.stageWidth - offset)
                    tempBomb.x = stage.stageWidth - offset;
                if (tempBomb.y > stage.stageHeight)
                    removeBomb(h);
    function makeEnemies():void
        var chance:Number = Math.floor(Math.random() * 150);
        if (chance <= level && enemies.length < 4)
            tempEnemy = new Enemy();
            tempEnemy.x = Math.round(Math.random() * 480);
            tempEnemy.cacheAsBitmap = true;
            addChild(tempEnemy);
            tempEnemy.scaleX = 1.5;
            tempEnemy.scaleY = 1.5;
            enemies.push(tempEnemy);
            tempEnemy.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
            if (tempEnemy.speed > MAX_SPEED)
                tempEnemy.speed = MAX_SPEED;
    function moveEnemies():void
        var tempEnemy:MovieClip;
        for (var i:int =enemies.length-1; i>=0; i--)
            tempEnemy = enemies[i];
            if (tempEnemy.dead)
                score++;
                score++;
                roachLevel.score_txt.text = String(score);
                enemies.splice(i,1);
            else
                tempEnemy.rotation += (Math.round(Math.random()*.4));
                tempEnemy.y +=  (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
                if (tempEnemy.x < 10)
                    tempEnemy.x = 11;
                if (tempEnemy.x > stage.stageWidth - offset)
                    tempEnemy.x = stage.stageWidth - offset;
                if (tempEnemy.y > stage.stageHeight)
                    removeEnemy(i);
                    lives--;
                    roachLevel.lives_txt.text = String(lives);
    function makeHearts():void
        var chance:Number = Math.floor(Math.random() * 8000);
        if (chance <=  +  level)
            tempHeart = new Heart();
            tempHeart.x = Math.round(Math.random() * 480);
            tempHeart.cacheAsBitmap = true;
            addChild(tempHeart);
            tempHeart.scaleX = 1.5;
            tempHeart.scaleY = 1.5;
            hearts.push(tempHeart);
            tempHeart.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
    function moveHearts():void
        var tempHeart:MovieClip;
        for (var k:int =hearts.length-1; k>=0; k--)
            tempHeart = hearts[k];
            if (tempHeart.dead)
                lives++;
                roachLevel.lives_txt.text = String(lives);
                hearts.splice(k,1);
            else
                tempHeart.rotation += (Math.round(Math.random()*.4));
                tempHeart.y +=  (Math.cos((Math.PI/180)*tempHeart.rotation))*tempHeart.speed;
                if (tempHeart.x < 10)
                    tempHeart.x = 11;
                if (tempHeart.x > stage.stageWidth - offset)
                    tempHeart.x = stage.stageWidth - offset;
                if (tempHeart.y > stage.stageHeight)
                    removeHeart(k);
    function makeAtoms():void
        var chance:Number = Math.floor(Math.random() * 7500);
        if (chance <=  +  level)
            tempAtom = new Atom();
            tempAtom.x = Math.round(Math.random() * 480);
            tempAtom.cacheAsBitmap = true;
            addChild(tempAtom);
            tempAtom.scaleX = 1.5;
            tempAtom.scaleY = 1.5;
            atoms.push(tempAtom);
            tempAtom.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
    function moveAtoms():void
        var tempAtom:MovieClip;
        for (var c:int =atoms.length-1; c>=0; c--)
            tempAtom = atoms[c];
            if (tempAtom.dead)
                score++;
                score++;
                score++;
                score++;
                score++;
                roachLevel.score_txt.text = String(score);
                atoms.splice(c,1);
            else
                tempAtom.rotation += (Math.round(Math.random()*.4));
                tempAtom.y +=  (Math.cos((Math.PI/180)*tempAtom.rotation))*tempAtom.speed;
                if (tempAtom.x < 10)
                    tempAtom.x = 11;
                if (tempAtom.x > stage.stageWidth - offset)
                    tempAtom.x = stage.stageWidth - offset;
                if (tempAtom.y > stage.stageHeight)
                    removeAtom(c);
    function removeEnemy(id:int)
        removeChild(enemies[id]);
        enemies.splice(id,1);
    function removeHeart(kd:int)
        removeChild(hearts[kd]);
        hearts.splice(kd,1);
    function removeBomb(hd:int)
        removeChild(bombs[hd]);
        bombs.splice(hd,1);
    function removeAtom(cd:int)
        removeChild(atoms[cd]);
        atoms.splice(cd,1);
    function testForEnd():void
        if (score > level * 20)
            level++;
            roachLevel.level_txt.text = String(level);
        if (lives == 0)
            gameState = STATE_END_GAME;
    function endGame():void
        removeGame();
        roachLevel.visible = false;
        Menu_mc.visible = false;
        endscreen_mc.visible = true;
        removeEventListener(Event.ENTER_FRAME, gameLoopR);
        showresults();
    function removeGame():void
        for (var i:int = enemies.length-1; i >=0; i--)
            removeEnemy(i);
        for (var h:int = bombs.length-1; h >=0; h--)
            removeBomb(h);
        for (var k:int = hearts.length-1; k >=0; k--)
            removeHeart(k);
        for (var c:int = atoms.length-1; c >=0; c--)
            removeAtom(c);
    The game works perfectly on Android and everything works. There is one problem though. The game gets laggy and the enemies get harder to click when the enemies' speed increases. This is the code for the enemies. It is a movie clip that gets added on to the stage by actionscript.
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    import fl.motion.Animator;
    import flash.events.*;
    play();
    var mysound:squish = new squish();
    this.addEventListener(MouseEvent.CLICK, kill);
    this.dead = false;
    function kill(e:MouseEvent):void
        this.dead=true;
        mouseChildren=false
        mysound.play();
        gotoAndPlay(21);
        this.removeEventListener(MouseEvent.CLICK, kill);
        flash.utils.setTimeout(removeSelf,2000);
    function removeSelf():void
        this.parent.removeChild(this);

    no,
    var identityMatrix:Matrix=new Matrix()
    my_sprite.cacheAsBitmapMatrix=identityMatrix;
    though for tempAtom,
    var scaleMatrix:Matrix=identityMatrix.scale(1.5,1.5); //would be appropriate.
    you can get a cc trial, http://gaming.adobe.com/technologies/scout/

  • How can i make it so more then one computer can be on time machine at one time

    How can I make apple airport let more the one computer be on the internet at one time

    Any computer logged into your wireless network should be able to access the Internet.
    Is that not the case.

  • How can I make the cursor more visible?

    I have seen in some training videos a ring around the cursor to make it more visible. How can I get this ring around my cursor in Photoshop CS 3?
    Anyone knows? /Tommy

    This should do it:
    1. Go to systems preference  and click it.  (it'll be under the top L hand side, click the apple logo and "System Preferences" will appear in a drop menu)
    2. Go to Show All
    3. Click Universal (round blue shape with a person logo)
    4. Click Mouse
    5. Go to Cursor size (at the bottom of the page)  and adjust the size from normal to large.  Watch your actual cursor on screen so you can see how big or small you're making it.
    voila:   a change of arrow or cursor size.

  • How can i make my code a working program?

    Hi again,
    I have been doing java for almost a year and have written over 100 programs now. IF i have a program i like how do i make it clickable from the desktop and jsut use it for fun. For example if i created a tic tac toe program how do i let people click on it and play it. Instead of having to compile it first?
    Thanks in advance for clarifying this for me because i haven't gotten to the point where our programs are stand alone.
    Thanks,
    MS

    You can create an executable jar file. But a JVM is needed to run it.
    http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/jar.html

  • WIFI WPA-EAP won't connect as EAP method only shows SIM, how can I make it show more?

    Trying to connect to a wifi connection which uses WPA-EAP security, with a username and password system (at work and I cannot change these settings).
    My Android device connects with no issues, but my ZTE open C on Firefox OS will not...
    Looking into my Android settings it is using EAP method PEAP; but the FFOS will only let me use SIM.
    What is the workaround for this?
    How can I connect my device to my work's wifi?

    GAUTAM24, if you read the earlier reply you would see a relevant bug has already been filed.

  • Menu Bar contrast is sick. How can I make it look more vivid?

    How can I increase the contrast for the Menu Bar?

    Maybe look if you can find a solid persona that works for you.
    *Personas for Firefox | Gallery: http://www.getpersonas.com/en-US/gallery/Solid

  • How can I make this code faster? optimized.

    Its part of two jsp pages that go back and forth. I feel I am creating new strings all of the time. I have 3 graphic representations of a radion button and a check box. its for a quiz program.
    if the button is not checked, it uses rd0.gif.
    if the button is checked, then its assigned rd1.gif.
    However, if the button is the wrong answer, then its assigned rd2.gif.
    the use of char arrays is neccasary, but not vital, I guess I could use indexof in the string. But I Dont know if its faster. I want to optimize the code, so it uses less resources.
    heres the code
    TrueFalse=entry[11].indexOf("TRUE");
    myButton = Integer.parseInt((String)session.getAttribute("BUTTONNO"));
    GivenAnswer =(String)session.getAttribute("GIVEN");
    if(TrueFalse==-1) {
    char[] questions = GivenAnswer.toCharArray();
    for(x=1;x<5;x++) {
    if(questions[x]=='1') { bp[x]="images/ck1.gif"; }else{ bp[x]="images/ck0.gif";}
    }else{
    if(myButton==1) {bp[1] = "images/rd1.gif";} else {bp[1] = "images/rd0.gif";}
    if(myButton==2) {bp[2] = "images/rd1.gif";} else {bp[2] = "images/rd0.gif";}
    questions is discarded.
    the quiz is fast at the begining, but once you go deeper and deeper it slows down. This is the second page:
    <%@ include file ="include4.jsp" %>
    <%
    session.setAttribute("BUTTONNO", "1");
    GivenAnswer =(String)session.getAttribute("GIVEN");
    char[] questions = GivenAnswer.toCharArray();
    questions[1]='1';
    tempString = new String (questions);
    GivenAnswer = tempString;
    session.setAttribute("GIVEN", GivenAnswer);
    %>
    <jsp:forward page="train2.jsp">
    </jsp:forward>
    I tried to keep all of the code on one page, but I am using images instead of real radio buttons, for several reasons. JSP doesnt seem to return a value for check/radio buttons in JSP, so clicking on an anchored image and going to a second JSP PAge seemed reasonable. I dont know how to force garbage collection. But the speed in which the pages and images appear, get slower and slower. so I Think im using up memory (Stings) at a greater rate than I should.
    thanks
    MIKe NIEMI
    [email protected]

    I'm not sure that an "optimisation" is necessarily what you're after. I doubt it's the creation of Strings that's causing any performance issue - even if it is then it's probably the result of something more sinister.
    Don't assume that your code is using a lot of resources.
    For example it's tempting to change it to this:
    if(myButton==1) {bp[1] = "images/rd1.gif";} else {bp[1] = "images/rd0.gif";}
    if(myButton==2) {bp[2] = "images/rd1.gif";} else {bp[2] = "images/rd0.gif";}
    // change to this?
    bp[myButton] = "images/rd1.gif";
    bp[3 - myButton] = "images/rd0.gif";I'd say that the original code was clearer and used just as many resources (once the Strings are interned).
    The same goes for this:
    if(questions[x]=='1') { bp[x]="images/ck1.gif"; }else{ bp[x]="images/ck0.gif";}
    // change to this?
    bp[x] = "images/ck" + questions[x] + ".gif";Again, the former is possibly clearer as it indicates that questions is really a boolean flag - it should also be easily optimised by the compiler.
    You're right that you could replace "questions" by simply using GivenAnswer.charAt(x).
    Enterprise Java applications can generate hundreds of thousands of Strings during their execution - your Servlet is unlikely to have a serious impact on that unless you've got an enormous loop in there or a very large user base.
    A golden rule of optimisation is not to assume that you know where the performance/resource bottlenecks are. Measure the improvement you get from a change - does it warrant the resultant increase in the complexity of the code?
    Put some profiling in your code - how long is it taking to run your Servlet? How many times is it being invoked? I can't imagine that the code you've provided will take more than a few milliseconds to run.
    Also, you can't force garbage collection. You can suggest it to the Java runtime (it might not even have a garbage collector!). However, this generally indicates a flaw in the application and should be avoided unless absolutely necessary (and it's not in this case!).
    If you really want to optimise then get hold of something like OptimizeIt - it will provide a great deal of information about what's being created and where.
    Hope this helps.

  • How can I make my code in the extractor just run 1x instead of on each pack

    I would like to know if there is a setting or some sort of code I can use to make sure my start routine executes after all data packages arrive.    We think it is missing data or reading it correctly because it is in different packages.

    Hi,
    If you are using function module generic extraction and using PACKAGE SIZE S_S_IF-MAXSIZE, then comment it or remove it wherever necessary. Then it should bring it in one single datapack from the source itself.
    Hope that helps. Thanks!!
    ~ Vaishnav

  • HELP: how can we make BPEL PM more developer friendly

    Hi All,
    With the new year starting, one of our resolutions for 2005 is to enhance the developer experience for BPEL PM and we are looking for suggestions and help in prioritizing them. Given that some of you guys have kicked the tires, you are the best source for those suggestions. I hope that you guys will be vocal.
    We are interested in the end to end experience: OTN, Download, 1 st hour, documentation, product development, console, this forum, anything else that is part of the overall experience.
    If you do not like to post, please just send me an email.
    Thank you!
    Edwin
    VP Product Development
    Oracle BPEL Process Manager
    [email protected]

    Edwin,
    I might have several suggestions as I'm sinking my teeth into the PM Server and Designer. We're considering to use the BPEL technology in the BIG way.
    First of all, make it a bug-free (as much as possible). Let me describe to you what I believe is a bug or a flaky design. In the Designer, File&gt;New&gt;Oracle BPEL Project creates a new project with a file bpel.xml. For example, look at your CreditFlow sample. In that file there is &lt;partnerLinkBinding name="..." &lt;property name=wsdlLocation"&gt; http://&lt;host name of the WSDL host server&gt;/....
    when the partner WSDL is hosted on the localhost that &lt;host name&gt; in the line above is the name of my machine instead of 'localhost'. Then, &lt;project name&gt;.bpel validation crashes when you're in the off-line (disconnected) mode or connected via VPN gateway (since obviously you think you don't need a proxy because everything is local). But, even if it works inside the LAN I think it's a bad design; since it's generated automatically by Designer the developer doesn't have a clue what's wrog when he/she needs to debug your code. The Designer should stick there 'localhost' instead of the the name of my computer on the network.
    Here how I spotted it. I generated you CreditFlow sample in the Quick Start Tutorial. I went by the book, Compiled the CreditFlow.bpel and it crashed! The book did not say it was supposed to crash. The book said everything should be honky dory. The Validator printed a message: "connection timeout...." Since I'm new to the BPEL it took me awhile to figure out and once I stick 'localhost' in the right place in bpel.xml everything worked. Again, I was running everything on localhost.
    So, I think it's a bug at worst or a flaky design at second. What am I missing here?
    The problem is that if my company wants to convert the Biz Analysts into BPEL modelers how could a BA figure out the solution to the problem like this? He/She would just give up and tell the Technologists (us) that this is a bad product.
    Thanks,
    Greg
    P.S. What happened to Doron Sherman (CTO) and the old Collaxa folks? He is not listed in the ORCL directory. For those who don't know BPEL Server and Designer were developed by Collaxa and ORCL bought that company last year.

  • How can I make Preview behave more like it used to?

    I use Preview a LOT for doing things like annotating screenshots for IT helpdesk stuff.
    In 10.9 it was invaluable; but the version in 10.10 seems to be a bad  step backwards. I used to be able to select a tool, like text,  a line, or oval, for example, and it would stick, and let me draw the item where I wanted it to go, like an actual graphics program.
    Now I have to reselect the tool each time, and it plops some default shape in the middle of the image that I then have to drag and resize to suit my needs. Needless to say this is making my task annoyingly repetitive and longer than necessary. The preferences are useless. Any hints or secrets to m,making Preview a useful graphics tool again?

    I use Preview a LOT for doing things like annotating screenshots for IT helpdesk stuff.
    In 10.9 it was invaluable; but the version in 10.10 seems to be a bad  step backwards. I used to be able to select a tool, like text,  a line, or oval, for example, and it would stick, and let me draw the item where I wanted it to go, like an actual graphics program.
    Now I have to reselect the tool each time, and it plops some default shape in the middle of the image that I then have to drag and resize to suit my needs. Needless to say this is making my task annoyingly repetitive and longer than necessary. The preferences are useless. Any hints or secrets to m,making Preview a useful graphics tool again?

  • How can i improve this code ?

    DATA: t_bkpf TYPE bkpf.
    DATA: t_bseg type bseg_t.
    DATA: wa_bseg like line of t_bseg.
    select * from bkpf into t_bkpf where document type ='KZ' and bldat in s_bldat.
    select single * from bseg into wa_bseg where belnr = t_bkpf-belnr.
    append wa_bseg to t_bseg.
    endselect.
    loop at t_bseg into wa_bseg.
      at new belnr.
         if wa_bseg-koart EQ 'K'.
            // pick vendor wrbtr
         else
           // pick other line item's wrbtr
         endif.
      endat.
    endloop.
    i am guessing my select statements arnt efficient performance wise, secondly in the loop when i use  'at new belnr' it aint showing my any values  whereas i get all the vendors(KOART EQ 'K') when i dont use 'at new belnr' .
    why is this so and how can i make the code efficient ?
    Thanks..
    Shehryar

    Hi,
    1.Dont read all the fields from the table unless it is required in your program.
    This will tremendously improve your performance.
    2.Make use of the key fields of the table wherever possible
    In your scenario you could use the fields BUKRS,BELNR,GJAHR of the table BKPF in the WHERE Clause rather than
    other fields.This will improve your performance a lot..
    3.As BSEG is a cluster table it will cause performance problem in most cases.So try to read
    the required fields from it rather than reading all the fields.Again Make use of the key fields in the WHERE Clause
    here too to improve the performance..
    4.Remove SELECT..ENDSELECT and replace it with FOR ALL ENTRIES to improve the performance.
    Cheers,
    Abdul Hakim
    Mark all useful answers..

  • How can I make a server differ between two or more clients?

    How can I make a server differ between two or more clients?
    The clients can connect and talk to the server fine, but how can I make the server talk to one, two or all clients? i.e. what would be a good way to implement this?
    Currently, the server listens for connections like this:
    while (listening) {
    try {
    new ServerThread(this, serverSocket.accept()).start();
    I guess one way would be to add the ServerThreads to a Hashtable with the client ID as key, and then get the ServerThread with the proper client ID, but this seems unnecessary complicated. Any ideas?

    Complicated was perhaps the wrong word, I should have
    written something like it doesn't "feel" right. Or is
    this a common and good way to solve communication
    between a server and multiple clients?Thats pretty much how I do it. I normally use an array or ArrayList of Sockets instead of HashTable, with [0] being the first player etc.... Then you can communicate with exactly who you want. If you want to send bytes to all of them, just send the same thing to each socket individually (or is there a better way to do this?).

  • How can you make your persona show on the top and bottom? Also can you make it bigger at the top to show more of the picture?

    How can you make your persona show on the top and bottom?
    Also can you make it bigger at the top to show more of the picture?

    Add extra empty toolbars if you want to see more of the persona.<br />
    You can create extra toolbars to get extra space via View > Toolbars > Customize<br />
    You have to put something on a toolbar (drag a Space item onto it) before closing the Customize window because empty toolbars are automatically removed.<br />
    See http://kb.mozillazine.org/Toolbar_customization

  • How can I make the Lion iCal display more readable?

    How can I make the Lion iCal display more readable?

    I'm wondering the same thing.
    Under Lion, my calendars' color coding is next to useless because they're suddenly so muted.
    Previously I could easily pick out which events belonged to which calendar without effort. Now, all the colors are so light as to be nearly indisinguishable; I must toggle calendars off and on to highlight a calendar's events.

Maybe you are looking for