• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to execute Scriptlet code on onclick of link.

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I want to copy files from one folder to folder when i click on hyperlink in my jsp.For that i used scriptlet code in javascript function.But it is executed when jsp body is loading.
Below is my code.Please help me to resolve this?

<html>
<head>
<script type="text/javascript">
function copyjscssfile()
{
alert("onclick event in if..filename ");
<%
File srcdir = new File(tabimpldir);
File src = new File(tabimpldir+"/jsp/stylesheets/myOwn");
File dest = new File(tabimpldir+"/jsp/stylesheets/smdasari");
if(src.isDirectory())
{
File[] listfiles=src.listFiles();
for(int i=0;i<listfiles.length;i++)
{
try {
if(dest.exists())
{
File destfile=new File(dest,listfiles[i].getName());
System.out.println("Copy.main() srcfile"+listfiles[i].getName()+" destfile"+destfile);
InputStream in =new FileInputStream(listfiles[i]);
OutputStream outs = new FileOutputStream(destfile);
byte[] buffer =new byte[1024];
int len=0;
while ((len = in.read(buffer)) > 0) {

outs.write(buffer, 0, len);

}

in.close();

outs.close();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
%>

}
</script>
</head>
<body>
copycssFiles
</body>
</html>
 
Ranch Hand
Posts: 138
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any specific reason why you want this code in JSP itself? Scriptlet code goes in service method of compiled JSP (Java class) so it will get executed when page is executed.

You can place that logic in a servlet, Servlet should get invoked on onclick event, by using form.submit.action of Javascript.
 
SaiManasa Dasari
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi acharya,
Thanks for Your reply.I want to execute this code on onclick event only.Those all copying files are css files which i want to load after refreshing jsp page dynamically when user clicks on particular link.Please help me how to do it?i dont know much about jsp and javascript.

Thanks in advance.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You fundamentally misunderstand how JSPs work. Please read this article.

If you want to trigger the execution of server-side code from within an HTML page (which is what a JSP page turns into once it reaches the browser), then you need to make an XHR call from JavaScript code, possibly using a framework such as Prototype or jQuery.
 
Prajakta Acharya
Ranch Hand
Posts: 138
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would prefer calling me by my first name, Prajakta.

Regarding your query, you can do something like this.

1) Execute a javascript function on onclick event of the link.
2) In that function, submit your form and set the URL pattern such that a servlet will be invoked.
E.g.

URL pattern abc.htm should be mapped to a servlet.
3) In the servlet code, you can get reference to web content files by using request.getRealPath("resourceName");
THere might be some other ways as well to do get this reference, not sure at the moment.
Then you can proceed with copy functionality.

Hope this helps.
 
Prajakta Acharya
Ranch Hand
Posts: 138
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you dont want complete page to be submitted on onclick event, you can go for Ajax.
 
SaiManasa Dasari
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ulf/prjakta,
Thanks for your reply.
I dont know ajax calling.is it possible through jquery?please help me is there any other way to do it in simple way.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SaiManasa Dasari wrote:
I dont know ajax calling.is it possible through jquery?


Yes. In fact, jQuery makes Ajax almost trivial to use. Without jQuery assistance, it can be quite challenging.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic