Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call javascript from servlet in java?

I want to call a function of javascript from servlet.

servlet code:

File ff = new File(uploadedFile+"/"+fileName+".mp4");

FileOutputStream fileOutSt = new FileOutputStream( ff );

fileOutSt.write(data);

fileOutSt.close();

request.setAttribute("src", ff);

RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsfunction.js");
dispatcher.include(request, response);

my javascript code:

myfunction(fileInput)
{
  var fileUrl = window.URL.createObjectURL(fileInput);
}

The problem is javascript calls but it display the code content but not execute it. how can i get fileURL.

like image 643
monika Avatar asked Aug 16 '26 09:08

monika


1 Answers

Several things are wrong here:

First, the inclusion of your javascript source is improper, because the javascript must be included (or referenced) always within an HTML file. In your case, instead, you are serving a MP4 file.

If you must absolutely execute that js code (remember that js is always executed in a browser), I suggest you serve an HTML page instead. In this case, the jsfunction.js script must be referenced within the HTML code:

<html>
<head>
<script type="text/javascript" src="jsfunction.js" />
</head>
<body>
...
</body>
</html>

Second: Even if you include the script, you must then invoke your function. You can call it immediately, from a scriptlet, or as a response to some client event (onclick, onload, etc).

like image 200
Little Santi Avatar answered Aug 18 '26 00:08

Little Santi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!