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

sendredirect not working as expected.  RSS feed

 
Raj Puri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at the code below. My intention is that when I click submit sendredirect should send me to another URL. But seems as soon as the form loads it send me to redirected URL.

Can someone sya what is wrong with this code?


----------------->
<html>
<head>
<script language="javascript">

function call_report() {

<% response.sendRedirect("http://localhost:9050/myapplication/xxx.jsp"); %>
return true;

}



<head>

</head>
<body >
<table>
<form name=repform method="post" onsubmit="return call_report()" >


<tr>
<td align="center"><input type="submit" style="font-weight: bold" name="Submit" value="Submit" ></td>
<td align="center"><input type="reset" name="Reset" style="font-weight: bold" value="Reset"></td>
</tr>

</form>


</table>
</body>
</html>
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javascript functions get executed on the client side.
JSPs write the page (including the JS functions) on the server side.

As soon as the page is shown in the browser, all the JSP functionality is done.

In your case, the sendRedirect is being called as soon as it's seen (the first load).

You can either implement this in Javascript document.location.url = ".." or something like that.

OR

You can implement it on the server by testing to see if the the form has been submitted yet. If so, redirect. If not show the page.

In the top of your page:
 
Raj Puri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for clearing it so well. So any code in Javascript gets executed
irrespective of its event? Kind of little confusing for me as I was expecting code to be executed on that javascript event(onsubmit here). That definitely seems to be the case here.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, The Server side code is fired on the server and the client side code is fired on the client.

If you wanted to do it with JavaScript, you would have to use JavaScript not the server side code.

e.g.


Eric
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Puri:
Thank you for clearing it so well. So any code in Javascript gets executed
irrespective of its event? Kind of little confusing for me as I was expecting code to be executed on that javascript event(onsubmit here). That definitely seems to be the case here.


No, think of it as the JSP getting fired first, on the server, before the Javascript interpreter ever sees it.

As far as the JSP engine is concerned your Javascript function is just text.
The first instruction that it saw (mixed in with the other text) was your sendRedirect.

Another way to think of it: Your JSP is writing your HTML and Javascript Code.
[ February 17, 2005: Message edited by: Ben Souther ]
 
evan theon
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't u use the 'action' parameter from the form? it simply direct u to the page u specified in the action parameter.

<form name="form1" method="POST" action="the_page_u_wish.jsp">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</form>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
evan,

JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

thanks,
bear
JSP Forum Bartender
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!