Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to execute Scriptlet code on onclick of link.  RSS feed

 
SaiManasa Dasari
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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>
 
Prajakta Acharya
Ranch Hand
Posts: 138
Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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
Java Spring
  • Mark post as helpful
  • send pies
  • 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
Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!