aspose file tools*
The moose likes JSP and the fly likes Good use of JSP for populating textfields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Good use of JSP for populating textfields" Watch "Good use of JSP for populating textfields" New topic
Author

Good use of JSP for populating textfields

Carlos Herrera
Greenhorn

Joined: Jul 09, 2008
Posts: 5
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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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 ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good use of JSP for populating textfields