简体   繁体   中英

Calling Javascript function from Servlet

This is my situation:

I want to call jAlert(String) Javascript method from a servlet linked to a JSP file.

I tried with the following

response.getWriter().println("<script type='text/javascript'>jAlert('Hello');</script>");

but there are no results.

After, I found a tool called Rhino , from Mozilla, but I don't know how to use. Help!

Thanks.

The first thing you should learn before starting a web application development is difference between client side and server side.

Your code will only render the tag which suggest browser to invoke that method. but it will not render that method so there will be a method not found error on browser (client side ) and that to only if browser desides to go just with script tag and invoke jAlert('Hello'); .

and for your question there is no way of invoking client side javascript method from servlet (Server Side).

Rhino is a javascirpt engine in java which is used to run javascrpt. but I don't understand why you want to execute javascript method on a JSP. Rhino being a java library the code will get executed on server side.

response.getWriter().println("<script type='text/javascript'>jAlert('Hello');</script>");

This above code will never be rendered on the browser since you are forwarding you request to a JSP page, either using RequestDispatcher or using sendRedirect method. In both option, response will only be generated for what you have written on your JSP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM