Call Javascript From Servlet In Java?
Solution 1:
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><scripttype="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).
Solution 2:
javascript plays on client side and Servlet plays on server side. You cannot execute Javascript on serverside. It should execute by browser.
I suggest you to make a javascript call in window onload.
Solution 3:
RequestDispatcher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. But not to JS. Since JS always runs in browser itself.
request.setAttribute("filename",filenamehere); //put filename
RequestDispatcher requestDispatcher;
requestDispatcher = request.getRequestDispatcher("/filename.jsp");//dispatch here
requestDispatcher.forward(request, response);
In filename.jsp
String value = (String)request.getAttribute("filename");//getting filenameDo like this. This way we will get the file url.
How to pass response from servlet to html
Call your servlet in same html using ajax with jquery.
In servlet
//getting input from `html` pageStringuserName= request.getParameter("userName").trim();
//now process your request here//forward response to `html` page
response.setContentType("text/plain");
response.getWriter().write("your file url");
In html call this servlet using ajax
$.ajax({
url : 'yourservletaction',
data : {
userName : $('#userName').val()//if you want to send any input do like this
},
success : function(responseText) {
$('#ajaxGetUserServletResponse').text(responseText);//getting file url as response. so use this url in you js
}
});
Post a Comment for "Call Javascript From Servlet In Java?"