Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using form property inside scriptlet  RSS feed

 
kapil kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers.

I want to use a property of form inside my scriptlet in jsp

say inside or outside any javascript function

<% String str=%> document.forms[0].txtId.value

where txtId is a property of form
How can I achieve this...

Please Help me out.......
[ October 10, 2008: Message edited by: kapil kumar ]
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this on the same page that has the form? If so, it is impossible, JSP runs on the server to generate the HTML. The generated HTML gets sent to the client, the client displays the page, then the JavaScript runs and user interaction begins. So JSP occurs well before the javascript in time (and on a different machine,) so communication can be one way only on a page, from JSP -> HTML since that is the life-cycle of JSPs.

So to get the value back to the server you need to make a new request to the server adding the value to the request.

If you wanted to collect a value on a form and use it in the NEXT page displayed, then you can do that by submitting the form and using request.getParameter("param name"); You can even make the NEXT page display the same page again, but this time with the extra information available.

If you wanted a more rich user interaction like GMail or the new Yahoo! pages then you need to send the client-side form data to the server, and read the server's response to this information, and update the client side display page - all from javascript. You would use AJAX to do this.
 
kapil kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Steve..
I am new to jsp so can you please explain more clearly

Say I have the following code in appln.jsp
here I need to check whether a form field is empty/not and based on that have to include another jsp
Say
<%
if(txnId!=null)
{
%>
jsp:include detail.jsp
<%
}
%>

and I have the script function as :

function getTxn()
{
<% String txnId %> = <property from the form>
}

As you said first the scriptlet will be executed on the server and then
the generated html is sent to client and then the java script runs..
So the above getTxn function won't compile...
So I am not getting how to set the value of form in request and fetch it again and use it.
I want the value to be used in the same jsp as i have stated above.

Please reply...
Thanks in Advance.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kapil kumar:
Thanks for the reply Steve..
I am new to jsp so can you please explain more clearly

Say I have the following code in appln.jsp
here I need to check whether a form field is empty/not and based on that have to include another jsp
Say
<%
if(txnId!=null)
{
%>
jsp:include detail.jsp
<%
}
%>

and I have the script function as :

function getTxn()
{
<% String txnId %> = <property from the form>
}

As you said first the scriptlet will be executed on the server and then
the generated html is sent to client and then the java script runs..



No, I said ALL scriptlets and JSP code will be executed on the server, the HTML sent to the client, then displayed, then the javascript runs.

Originally posted by kapil kumar:
So the above getTxn function won't compile...
So I am not getting how to set the value of form in request and fetch it again and use it.
I want the value to be used in the same jsp as i have stated above.

Please reply...
Thanks in Advance.


You will have to submit the form and re-display the page with the values from the form. Or, you will have to use AJAX.

To resubmit the form, use a javascript function to submit the form when a value gets sent. The action that the form submits to would then read the parameters via request.getParameter("param name") and decide what to do - either re-display this same JSP page but with the included the second JSP, or do whatever you need if the complete form was filled out. How to do that depends a lot on your code and your configuration. I would suggest taking small steps:
1) Find out how to submit the form using javascript
2) Make a Servlet that you post your form to
3) Have the Servlet check the parameters and forward to the appropriate page
4) Redesign the JSP so it can check if the form field has already been set as a request parameter and do the work as needed.

I can't show you how to do it because it depends a lot on what your code already is.

The second option is the latest fad in rich web interfaces - AJAX. This is a subject unto itself. There are a lot of resources, guides, and frameworks out there to help you out. I would suggest that you at least investigate AJAX since it will be a nicer interface for your users.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following articles may help you understand JSP better:

Please read this article and this article.
 
kapil kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the support. Bear and Steve
I would refer to the mentinoed journals and try to clear my concepts..
and coe ack to you guys again if any issues.
Once again thanks...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!