Forums Register Login

Good use of JSP for populating textfields

+Pie Number of slices to send: Send
Hi all

I've got a piece of code for populating textfields that looks like this:

<blockquote>code:
<pre name="code" class="java">
function do(){
<%

String action = request.getParameter("action");

if(action != null && action == "A"){

MyBean theBean = MyJavaClass.myGetByMethod(request.getParameter("select_field"));

String text_field1 = theBean.getField1();
String text_field2 = theBean.getField2();
String text_field3 = theBean.getField3();
}
%>

document.forms[0].text_field1.value = '<%=text_field1%>';
document.forms[0].text_field2.value = '<%=text_field2%>';
document.forms[0].text_field3.value = '<%=text_field3%>';
}

function set_action(action_value){

document.forms[0].action.value = action_value;
document.forms[0].submit();

}
</pre>
</blockquote>

The do() function is called inside the body tag, through onLoad.

The set_action function is called inside the select field tag, through onChange, passing "A" as parameter.

I've also got a hidden input named "action".

MyJavaClass.myGetByMethod(String select_option) connects to a database and gets info based on the String that is passed as a parameter.

This code works fine for me. My question is: am I using jsp in a improper manner? Is there a better way to do this without using other technologies (Ajax for example)?

Thanks in advance
[ July 11, 2008: Message edited by: Carlos Herrera ]
+Pie Number of slices to send: Send
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
+Pie Number of slices to send: Send
If you think that the JSP code is executing as part of the JavaScript do() function, you are quite mistaken. That JSP code -- in fact, all JSP code on the page -- executes on the server long before the page gets sent to the client where the JavaScript gets a chance to run.

Do a View Source on your page once it gets to the browser and observe what the contents of the do() method have turned into.

It appears to work because the JSP has created JavaScript statements that contain the dynamic data. But it's important to realize that all JSP code ran on the server, long before the page got sent to the browser.

The way that you are doing this is highly inefficient though. Why create JavaScript statements to assign values when you can use the value attribute of the elements?
[ July 11, 2008: Message edited by: Bear Bibeault ]
+Pie Number of slices to send: Send
P.S. Your subject seems to have nothing to do with your post. Where does the <select> come in? The only reason that this appears to work is that you are calling do() at page load. So it's hard to tell the difference between things that happen on the server and things that happen just before the page is displayed.

If you need to make decisions in the JSP code based upon data and actions that can't be known until after the page is presented to the user (such as selections from a <select> element), your approach completely falls apart.

Try this. In your page, move the scriptlet out of the do() function and to the top of the file. Everything still works! The scriptlet is not part of the JavaScript. It doesn't matter where you put it on the page (as long as it's before the scriptlet expressions that references the variables it creates) because it executes on the server in order to help format the page. It's not part of the HTML/JavaScript in the least.
[ July 11, 2008: Message edited by: Bear Bibeault ]
What are your superhero powers? Go ahead and try them on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1130 times.
Similar Threads
How to link different buttons to different action methods
selecting multiple rows to update value
help for action?
displaying data from two table in single jsp page
Enabling / disabling forms (Struts)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:39:31.