Solved

Run JSP-code only when page is loaded

Posted on 2006-11-14
7
873 Views
Last Modified: 2008-01-09
Hi!

I have a basic question. How can I run some JSP-code only when a page is loaded?

Say that I dynamically create a select-box inside a form like this:
<form>
    <select>
    <%
      page.connect();
      ResultSet rs=page.getInfo();
      while (rs.next()) {
          String s=rs.getString("info");        
    %>
    <option value="<%= info %>"><%= info %>
    <% }
      page.disconnect();
    %>
    </select>
    <input type="submit" value="Vis">
</form>

This code should only be run once when the page is loaded, i.e. on "onload" in JavaScript-terms.

How can I do that?

Regards,
Jesper
0
Comment
Question by:jespersahner
7 Comments
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 39 total points
ID: 17940960
You can not. jsp code runs on the server & hence will finish executing even before your page is served on the client..
0
 
LVL 12

Assisted Solution

by:jessegivy
jessegivy earned 37 total points
ID: 17941242
The above esteemed expert is correct.  The JSP code you're showing will output text to the html file and as you can see, it's mingled in with your select element.  Perhaps you could explain what you want to accomplish by waiting until the page is loaded to retrieve the data?  You could include the code after the html tag and then fill the select element by ID using javascript, but it's fairly involved and wouldn't do it without good reason.

HTML code is executed sequentially so if you went with the javascript method you'll have an empty select element added to your page then fill it with options.  Again though, Why do you want to wait, what you have looks acceptable to me?

Cheers,

Jesse
0
 

Author Comment

by:jespersahner
ID: 17941325
I think you misunderstand me or I misunderstand you :-)

The idea is, that the select-box should be created only once/initialized) and not re-created with every click on the page. I am looking for the same functionality as with JavaScript using <body onload="some function()">, calling the "some function" only when the page is loaded.

Regards,
Jesper
0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 37 total points
ID: 17943923
Is your result set returning data in xml format?
 In that case , you have to remove the scriplet from jsp (which is a bad practise anyway)  and put the java code at the back, return the result(xml) from the java class and when the jsp page loads , onLoad call a javascript function which will load the xml data and traverse through the xml using xpath and populate the drop-down field.



0
 
LVL 29

Assisted Solution

by:bloodredsun
bloodredsun earned 37 total points
ID: 17955462
you could try using a static code block which will ensure that it is run once only or running it in a declaration <%! declaration(s) %>
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet concurrency 13 101
AlarmClock Challenge 35 150
withoutString  challenge 40 198
designing in object programming 12 76
SEO can be a real minefield to navigate, but there are three simple ways to up your SEO game just be re-assessing your content output.
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question