File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Javascript code inside JSP (error in flushing) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Javascript code inside JSP (error in flushing)" Watch "Javascript code inside JSP (error in flushing)" New topic
Author

Javascript code inside JSP (error in flushing)

shan hosur
Greenhorn

Joined: Mar 21, 2006
Posts: 4
Hello there,

In a JSP file, I have a "alert" message from javascript after that i am forwading to the new JSP file.
scenario : 1

I am not getting the alert message. But iam getting fwded to the new JSP page.

scenario : 2

If i remove that Forwarding code. "Alert" is coming.

I found this is something to do with flushing the data.

scenario : 3
So I included
response.flush() now iam able to display the "alert" but "Comitted JSP error" is coming instead of my new JSP.


I want the alert box to be displayed and fwded page as well. How do i do that? PLz help me.
Here is the code:

<%
System.out.println(" ******** error");
%>
<script language="JavaScript">
alert("assignerror");
</script>

<%
//response.flushBuffer();
System.out.println(" ******** error before forwarding...");
%>

<jsp:forward page="/help.jsp"/>

Thanks,
shan.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

"shan ms",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


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

Joined: Jan 10, 2002
Posts: 60046
    
  65

Originally posted by shan ms:
In a JSP file, I have a "alert" message from javascript after that i am forwading to the new JSP file.


I did not need to read any further than this to determine that you have a fundamental misunderstanding about how JSPs work.

The forward happens on the server long before the Javascript gets sent to the client for execution by the browser.

Please read this article for an explanation of how JSPs work and why you could never get the alert before the forward.
shan hosur
Greenhorn

Joined: Mar 21, 2006
Posts: 4
Hello there,

If i couldnt forward after JS Alert,shall i include a JSP page after an javascript alert? I was abel to do in weblogic7.1.
In websphere i am getting this error :

ServletRes E SRVE0157E: setBufferSize() called after first write to Output Stream/Writer
java.lang.IllegalStateException: setBufferSize() called after first write to Output Stream/Writer

Plz help me with some tips.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

"shan ms", My request that you change your display name to adhere to JavaRanch standards was not a suggestion. Valid display names are mandatory for participation on the Ranch. Please change your display name as instructed prior to your next post.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by shan ms:
Hello there,

If i couldnt forward after JS Alert,shall i include a JSP page after an javascript alert? I was abel to do in weblogic7.1.
In websphere i am getting this error :

ServletRes E SRVE0157E: setBufferSize() called after first write to Output Stream/Writer
java.lang.IllegalStateException: setBufferSize() called after first write to Output Stream/Writer

Plz help me with some tips.

Thanks.



No, by the time the response has made it to the browser, where the Javascript code is interpreted, all of the server side code is done.
If you want to do something on the server after a Javascript alert, you'll need to make a new request.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Did you read the article I referred you to?
shan hosur
Greenhorn

Joined: Mar 21, 2006
Posts: 4
Hi,

yes, I read that article. Hope you are happy with the name change now.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Originally posted by shan hosur:
yes, I read that article.


Then you now understand that JSP pages are templates, not programs that execute from top to bottom. The Jvaa code and JSP actions within them execute on the server in order to render an HTML/Javascript page which is sent to the client wehere the Javascript is executed.

Hope you are happy with the name change now.


Yes. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javascript code inside JSP (error in flushing)
 
Similar Threads
not getting alert msg in jsp
passing object from JSP to Javascript
Access denied error
read and parse an external xml using javascript
File Upload using commons-fileupload-1.0