从java中的servlet调用javascript?

编程入门 行业动态 更新时间:2024-10-08 22:52:38
本文介绍了从java中的servlet调用javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从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?

本文发布于:2023-11-28 17:56:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643392.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:java   servlet   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!