Emenor Emenor -4 years ago 50
Java Question

How to load javascript function after a servlet loads page using PrintWriter

I've been searching and I can't find an answer to this.

I'm using Servlet and after the servlet loads, I'm printing using

response.getWriter().print(String);
in the web. When all the content in the web browser is loaded I want to execute a Javascript script but I can't make it runs.

Any idea how can make it run?

Answer Source

Using response.getWriter().print(String) is very primitive way of generating HTML from servlet. Your best bet is to use JSP or JSF for that purpose. But to answer to your original question you either need to write the raw Javascript code or Javascript import along with onload right into the string that you pass to print method. See this HTML snippet for example how that string should look like:

<body onload="doSomething()">

<script>
function doSomething() {
}
</script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download