469,625 Members | 1,714 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Can I include JS in a JSP scriptlet?

157 100+
Hi All,

I have a dynamic table in my web page which I'm creating in a jsp-scriptlet -

<html>
<body>
<%
out.write("<table>") ;
// code to add rows dynamically to the table from my ResultSet
out.write("</table>");
%>
</body>
</html>

Would it be possible to include some javascript-code in the scriptlet like say -
out.write("<script>document.getElementById(\"divMa sk\").style.display=\'none\'</script>");

I did try the above but doesn't work neither gives any error and although I know that Jsp is executed on server-side and JS on client-side,but still wanted to know if there was any chance that this was possible to achieve?!
Feb 7 '07 #1
8 15921
Ganon11
3,652 Expert 2GB
This should probably be posted in the Javascript forum.
Feb 7 '07 #2
abctech
157 100+
This should probably be posted in the Javascript forum.

Hi and Thanks for your reply!

Well the thing is that if I were just to say something like --
<html><body>
<table>
</table>
<script>
//some code
</script>
</body></html>

then since its a static page the above code would work absolutely fine

But what I'm trying is --
<html><body>
<!-- scriptlet begins-->
<%
//some dynamic content developed using out.write()
out.write("<table>");
//creating and adding rows on the fly
out.write("</table>");
out.write("<script>some code</script>");
%>
<!-- scriptlet ends-->
</body></html>

The code inside the scriptlet is creating a table on the fly and I'm hoping to change some attributes of this dynamic table by using a JS inside the scriptlet itself.

I know it sounds a bit strange and I do know that JSP and JS both have different purposes and are run on server and client side resp. but wanted to know if I can actually use a script in the above fashion ,i.e inside the scriptlet?
Feb 7 '07 #3
dmjpro
2,476 2GB
sure u can do this .....
the out.write will generate the ultimate html file...
in side a html the js code can be like this.............
Feb 7 '07 #4
abctech
157 100+
sure u can do this .....
the out.write will generate the ultimate html file...
in side a html the js code can be like this.............
Yeah,I tried but its not working, I have dropped the idea and going another way now! But thanks for your reply!
Feb 7 '07 #5
dmjpro
2,476 2GB
u should add like this ................
<script language = javascript>code</script>
plz send me reply
i am online
Feb 8 '07 #6
abctech
157 100+
u should add like this ................
<script language = javascript>code</script>
plz send me reply
i am online
Hi,
Actually I put the <script> in my servlet saying -

PrintWriter pw = response.getWriter();
and then
some if() condition that I' have in my servlet
{
pw.println("<script>alert(\"This is some alert message\")</script>");
}

and its working but this servlet is connected to a Jsp,when I hit enter on the Jsp this particular servlet gets invoked and the strange thing is every time I load my Jsp or hit refresh then even if the if-condition isnt true still the alert message pops up , and it shouldn't be like that, I think I'm going wrong with the logic somewhere..

I guess these are the perils of trying to merge two diversely different languages without knowing too much about either of them...thats why I dropped the idea of using JS inside the servlet or scriptlet for the timebeing atleast..

I will surely think of something later coz I need to give that alert message to the user if the control enters that if-condition..will have to ponder upon it for a while..

Anyways,thanks again for your reply!

Cheers
Feb 8 '07 #7
dmjpro
2,476 2GB
sorry for knock again
check out the generated html code
don't min
can u give me the genrated html code or the total code u r using
i am waiting.....
Feb 8 '07 #8
abctech
157 100+
sorry for knock again
check out the generated html code
don't min
can u give me the genrated html code or the total code u r using
i am waiting.....
generated Html code as in?
Feb 8 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Lim Chee H | last post: by
3 posts views Thread by eddie | last post: by
9 posts views Thread by unlikeablePorpoise | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.