从java中的servlet调用javascript? [英] call javascript from servlet in java?
问题描述
我想从servlet调用javascript函数。
I want to call a function of javascript from servlet.
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);
我的javascript代码:
myfunction(fileInput)
{
var fileUrl = window.URL.createObjectURL(fileInput);
}
问题是javascript调用,但它显示代码内容但不执行它。
我怎样才能获得fileURL。
The problem is javascript calls but it display the code content but not execute it. how can i get fileURL.
推荐答案
这里有几个问题:
首先,包含您的javascript源是不合适的,因为必须在HTML文件中包含(或引用)。在您的情况下,您正在提供MP4文件。
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.
如果您必须绝对执行该js代码(请记住,js始终执行在浏览器中),我建议你改为提供HTML页面。在这种情况下,必须在HTML代码中引用jsfunction.js脚本:
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>
第二:即使你包含了脚本,你也必须调用你的功能。您可以立即从scriptlet中调用它,或者作为对某些客户端事件的响应( onclick
, onload
等) )。
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).
这篇关于从java中的servlet调用javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!