user3623843
user3623843

Reputation: 29

Calling JavaScript function from Servlet, Defined in JSP page

i have a JavaScript function

function ShowDeptPanel()
{

document.getElementById("table1").style.visibility="hidden";
document.getElementById("table2").style.visibility="visible";
}

which is defined in JSP page, but i want to call it from Servlet. How can i do this ?

Note: i want to disable 1 table after a certain check in my Servlet Code.

Upvotes: 1

Views: 1669

Answers (2)

mohamedrias
mohamedrias

Reputation: 18576

In your JSP, use scriptlet to call the servlet function and get the result.

If we consider it to be boolean value, then

<script type="text/javascript">
function ShowDeptPanel()
{

document.getElementById("table1").style.visibility="hidden";
document.getElementById("table2").style.visibility="visible";
}

var serverCheck = <% JavaClass classObject = JavaClass.getInstance();
                         classObject.getMethodChecking();%>;

if(serverCheck==="true") {
    ShowDeptPanel();
}
</script>

This is one way to hide the table. If you want it to be dynamic, make ajax call to the servlet and make the servlet return you the response which can be processed in javascript to hide / show the table.

Upvotes: -1

Alexandre FILLATRE
Alexandre FILLATRE

Reputation: 1327

Javascript is client-side, so it HAS TO be executed from the web browser. If what you want to do is execute javascript at the page loading, after a specific servlet has been executed, you can do the following :

  1. In the servlet, add a variable (like executeScript) in the request attributes
  2. In the JSP, check this variable's value, and execute the javascript if it's true

If you servlet always sends the same page, then just add you javascript function call in the onload attribute of your HTML <body> tag

Upvotes: 2

Related Questions