简体   繁体   中英

How to write jsp scriptlet inside javascript function

Hi my code is not working,

In javascript method, i have written jsp scriptlets.

function caller() {
<% out.println("scriptlet working in js method"); %>
}

I am not getting the output in 'server console'.

File is jsp file and running on websphere application server.

can anyone tell what is happening.

If you absolutely need to "run a scriptlet in javascript". Save what you need to run in your jsp file in a variable such as:

<script>
    jsp_myVarName = "<% out.println("scriptlet working in js method"); %>"; 
</script>

And then in your javascript file use that variable.

function caller() {
    console.log(jsp_myVarName);
}

All in all I would discourage you to use a scriptlet and use EL instead.

The fact is that JSP executes scriplets while loading the page (server side). What you are trying to do is to tell server to execute the code only when your client-side function is called.

For this, I recommend use of AJAX method - read this article :

https://www.w3schools.com/xml/ajax_xmlhttprequest_create.asp

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