1

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.

7
  • 1
    You can't call javascript function from servlet. May 10, 2014 at 17:12
  • 1
    @HAL9000 are you aware of what you're saying? May 10, 2014 at 17:14
  • @mohamedrias yes you can, by using ScriptEngineManager, but this is not the appropriate use case for it. May 10, 2014 at 17:14
  • then how to change content of JSP page, for example enabling disabling of content ? May 10, 2014 at 17:14
  • @LuiggiMendoza, any other alternative for that ?? May 10, 2014 at 17:17

2 Answers 2

2

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

-1

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.

5
  • scriptlet runs only once at rendering time of the JSP. After that, it becomes static in the generated HTML. May 10, 2014 at 17:17
  • Yes, I just showed him an example. Before the page is rendered, the scriplet will resolve so based on that the table will be shown or not. May 10, 2014 at 17:19
  • If he wants it to be dynamic, then he can have ajax call to call the servlet and get the value, based on that he can show or hide the table May 10, 2014 at 17:19
  • Also, this won't generate the expected results you want to show to OP. May 10, 2014 at 17:19
  • I assumed that the OP wants to hide the table and he doesn't know how to do it. So he thought of calling javascript function from the servlet. I need to read more about ScriptEngineManager. Anyways thanks for your suggestion:) May 10, 2014 at 17:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.