wood burning stoves 2.0*
The moose likes Servlets and the fly likes Javascript code in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Javascript code in servlet" Watch "Javascript code in servlet" New topic
Author

Javascript code in servlet

Kishore Shewani
Greenhorn

Joined: Jan 23, 2004
Posts: 14
Hi!!,
I am facing this little problem of executing javascript code via a servlet.
will the following code work:
(In put method of some servlet )

PrintWriter out = new PrintWriter(response.getOutputStream());
out.write("<HTMl><SCRIPT>window.open('index.htm')</SCRIPT></HTML)");
if yes , then how does this work?
Tips on using javascript code within Servlets are welcome too .


Toil and see your dreams coming true. <br />:-)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
JavaScript programs execute on the browser - in most cases.
Servlets execute on the server side to write HTML pages that may contain JavaScript, but the JavaScript does not execute on the server. So - don't say "using javascript code within servlets" - say "writing javascript code from servlets."
My advice is to create static HTML pages that contain the JavaScript you want - once that is working, try to get a servlet to write the same content. Just jumping into the middle will result in a big mess because there are so many things that can go wrong.
Bill
Kishore Shewani
Greenhorn

Joined: Jan 23, 2004
Posts: 14
Thanx William,
I agree with your opinion. The problem out here is i am dealing with some pop-up windows which must be closed on some server side event.
Like
if ( data not found ){
<SCRIPT> self.close() </SCRIPT>
}
else{
<SCRIPT> opener.location.href = url </HTML>
}
while the above works fine , it becomes a problem if i try to write more than one javascript statements on single event.
Like,
if ( data not found ){
<SCRIPT> opener.close();self.close(); </SCRIPT>
}
Any suggesstions.....

Regards!

Kishore
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Yow! That sort of thing gets tricky. Of course a server must receive a request before it can tell your pop-up window that the data was not found - that gets you involved with having JavaScript in the pop-up generate a request to the servlet - the servlet would then generate either the script to close the window or the desired content.
If my problem looked like that I would seriously consider a redesign of the interface.
Bill
 
Consider Paul's rocket mass heater.
 
subject: Javascript code in servlet