Reading .js file into JSP code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Iain Downie

    Reading .js file into JSP code

    Anyone out there know of an easy way to fill a Java ArrayList in a JSP from
    an
    array of strings held in a external JavaScript file? We need to load up a
    large number of strings into the page and manipulate with JSP, and feel that
    the best way is for the users to have the bulk of the text 'cached' on their
    machine in a .js file rather than read from a database every time they
    access. The strings will rarely change.

    If not, any other better suggestions for cutting the speed of download?

    Thanks
    Iain

    --

    _______________ _______________ _______________ ______________
    Dr Iain Downie
    Web Software Developer, British Trust for Ornithology, The Nunnery,
    Thetford, Norfolk IP24 2PU, UK ® Charity No. 216652
    Tel: +44 (0)1842 750050, fax: +44 (0)1842 750030 Iain.Downie@bto .org

    BirdWeb Gateway: http://www.bto.org/birdweb


  • Andrew Thompson

    #2
    Re: Reading .js file into JSP code

    On Tue, 29 Jun 2004 15:17:25 +0100, Iain Downie wrote:
    [color=blue]
    > Anyone out there know of an easy way to fill a Java ArrayList
    > in a JSP ...[/color]

    On the server. Not so much the 'easy' way,
    as the 'only' way.
    [color=blue]
    > ...from an array of strings held in a external JavaScript file?[/color]

    If that JS file was on the server,
    you could load it and parse it for
    the strings, but the *easy* way is
    to define the strings as Java Strings
    in the JSP, or define the ArrayList
    itself in the JSP.

    *Whatever* you are trying to achieve,
    you are going about it the completely
    wrong way.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology

    Comment

    • Iain Downie

      #3
      Re: Reading .js file into JSP code

      After quite a bit of searching, I'm inclined to agree that this is pretty
      much impossible, as JavaScript is client and JSP server-side. What I am
      trying to *achieve* is get the strings in a file that is as fast as possible
      to use, and I thought having that cached on the clients machine would be
      best - now know otherwise.

      I think I'll have to follow your suggestion and find some way to predefine
      the list of strings in JSP, but without constant calls to the database.

      Thanks
      Iain

      "Andrew Thompson" <SeeMySites@www .invalid> wrote in message
      news:8t2iar8bcz hc.1c2cce58h4xh o$.dlg@40tude.n et...[color=blue]
      > On Tue, 29 Jun 2004 15:17:25 +0100, Iain Downie wrote:
      >[color=green]
      > > Anyone out there know of an easy way to fill a Java ArrayList
      > > in a JSP ...[/color]
      >
      > On the server. Not so much the 'easy' way,
      > as the 'only' way.
      >[color=green]
      > > ...from an array of strings held in a external JavaScript file?[/color]
      >
      > If that JS file was on the server,
      > you could load it and parse it for
      > the strings, but the *easy* way is
      > to define the strings as Java Strings
      > in the JSP, or define the ArrayList
      > itself in the JSP.
      >
      > *Whatever* you are trying to achieve,
      > you are going about it the completely
      > wrong way.
      >
      > --
      > Andrew Thompson
      > http://www.PhySci.org/ Open-source software suite
      > http://www.PhySci.org/codes/ Web & IT Help
      > http://www.1point1C.org/ Science & Technology[/color]


      Comment

      • Grant Wagner

        #4
        Re: Reading .js file into JSP code

        Iain Downie wrote:
        [color=blue]
        > Anyone out there know of an easy way to fill a Java ArrayList in a JSP from
        > an
        > array of strings held in a external JavaScript file? We need to load up a
        > large number of strings into the page and manipulate with JSP, and feel that
        > the best way is for the users to have the bulk of the text 'cached' on their
        > machine in a .js file rather than read from a database every time they
        > access. The strings will rarely change.
        >
        > If not, any other better suggestions for cutting the speed of download?
        >
        > Thanks
        > Iain[/color]

        Of course this is possible. Given the understanding that a JSP generates HTML
        dynamically, it isn't too much of a stretch to understand it can generate
        Javascript dynamically as well. So your starting point is:

        <script type="text/javascript"
        src="somethingT hatMakesAJavasc riptArray.jsp"> </script>

        Then somethingThatMa kesAJavascriptA rray.jsp would read the database and generate
        code that resembles:

        var yourArray = [
        "value1",
        "value2",
        "value3"
        // etc
        ];

        Doing this in server-side Javascript would be something like:

        <%
        // do the database retrieval into resultSet here complete
        // with error checking; if resultSet is properly populated
        // then proceed with the code below
        Response.write( 'var yourArray = [\n');
        var lastIndex = resultSet.rows - 1;
        for (var i = 0; i < resultSet.rows; i++) {
        resultSet.curso r = i;
        Response.write( '\t"' + resultSet.theCo lumn + '"');
        if (i < lastIndex) {
        Response.write( ',');
        }
        }
        Response.write( '];');
        %>

        But depending on the amount of data involved, downloading this huge array to the
        client might not be such a wise idea. Not to mention once the data is "cached"
        in a client-side Javascript array, it can't be manipulated by JSP, it has to be
        managed by client-side Javascript, something that might be problematic if you're
        supporting multiple browsers.

        --
        | Grant Wagner <gwagner@agrico reunited.com>

        * Client-side Javascript and Netscape 4 DOM Reference available at:
        *


        * Internet Explorer DOM Reference available at:
        *
        Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


        * Netscape 6/7 DOM Reference available at:
        * http://www.mozilla.org/docs/dom/domref/
        * Tips for upgrading JavaScript for Netscape 7 / Mozilla
        * http://www.mozilla.org/docs/web-deve...upgrade_2.html


        Comment

        • kaeli

          #5
          Re: Reading .js file into JSP code

          In article <cbs1jg$2h1$1$8 302bc10@news.de mon.co.uk>, iain.downie@bto .org
          enlightened us with...[color=blue]
          > After quite a bit of searching, I'm inclined to agree that this is pretty
          > much impossible, as JavaScript is client and JSP server-side. What I am
          > trying to *achieve* is get the strings in a file that is as fast as possible
          > to use, and I thought having that cached on the clients machine would be
          > best - now know otherwise.
          >
          > I think I'll have to follow your suggestion and find some way to predefine
          > the list of strings in JSP, but without constant calls to the database.
          >[/color]

          Why don't you just use an include file?
          Same thing as the JS file concept, but all on the server.
          JSP has an include directive.

          --
          --
          ~kaeli~
          Going to church doesn't make you a Christian any more than
          standing in a garage makes you a car.



          Comment

          • Iain Downie

            #6
            Re: Reading .js file into JSP code


            "kaeli" <tiny_one@NOSPA M.comcast.net> wrote in message
            news:MPG.1b4b9e 117595fe48989f4 1@nntp.lucent.c om...
            [color=blue]
            > Why don't you just use an include file?
            > Same thing as the JS file concept, but all on the server.
            > JSP has an include directive.[/color]

            I thought about that, and yes it is possible, but what advantages in
            download speed would there be? The original idea was the huge array of
            strings would have been held on the clients machine and thus not need as
            much download. An include file would always have to be recalled when the JSP
            is turned into HTML. The only advantage I can see with includes (in this
            case) would be easier coding and a single instance that I can update when
            required.

            Thanks
            Iain


            Comment

            • Andrew Thompson

              #7
              Re: Reading .js file into JSP code

              On Wed, 30 Jun 2004 08:41:56 +0100, Iain Downie wrote:[color=blue]
              > "kaeli" <tiny_one@NOSPA M.comcast.net> wrote in message[/color]
              ...[color=blue][color=green]
              >> Why don't you just use an include file?
              >> Same thing as the JS file concept, but all on the server.
              >> JSP has an include directive.[/color]
              >
              > I thought about that, and yes it is possible, but what advantages in
              > download speed would there be? The original idea was the huge array of
              > strings would have been held on the clients machine and thus not need as
              > much download.[/color]

              We have now determined that what you wish to
              achieve cannot be done with JS, so this thread
              should be continued elsewhere.

              I will give you a parting thought though.
              A signed Java applet can store data on the
              client for quick access..

              If you choose to use an applet, I suggest you
              make a 'follow-up' to this post, but direct
              it straight to c.l.java.progra mmer.

              HTH

              --
              Andrew Thompson
              http://www.PhySci.org/ Open-source software suite
              http://www.PhySci.org/codes/ Web & IT Help
              http://www.1point1C.org/ Science & Technology

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Reading .js file into JSP code

                Iain Downie wrote:[color=blue]
                > After quite a bit of searching, I'm inclined to agree that this is pretty
                > much impossible, as JavaScript is client [...]-side. [...][/color]

                No, J(ava)Script is a script language. It can be used both client-
                and server-side, provided that you have an appropriate server.


                PointedEars

                Comment

                Working...