1

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.

2
  • You can't call browser based Javascript from a Servlet. Servlets execute serverside, Javascript executes in the browser (assuming it is in HTML being rendered by your browser. What exactly are you trying to do? Commented Mar 20, 2015 at 6:14
  • did you also include the js library where the function is defined? response.getWriter().println("<script type='text/javascript' src='/path/to/js''></script>"); Commented Mar 20, 2015 at 7:13

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.