0

i desperately need some suggestion on how a javascript variable make available in Scriptlet. i know that javascript runs on client side where scriptlet code runs on the server. But i think there is some way to make the javascript variable available in scriptlet. below is my code.

Javascript fun

function showDirStructure(repoId, repoName){


        <% 
        String sr = repoId;
        if(sr!=null){
        JSONObject obj = getDirStructure.createJsonObject(request.getParameter("repoId")); 

         %>

The above javascript function (showDirStructure) takes two parameter. i need to use those parameter on Scriplet tag to call createJsonObject (server side method).

4 Answers 4

1

Scriptlet is executed before any data about webpage get sent from server to client. Whatever you want to do you need to send postback to server (with forms or ajax call). I usually use jQuery so my answer will use it but feel free to modify it to use native JS code. First, I would create a page on server called createJsonObject, call it from client with $.ajax (type: "POST")and passed my two arguments as object {repoId: repoId, repoName: repoName}. On server I would place my JSP on that page, read argumants upon page load, execute function and return object with data to client. In .done() I would do something with that data (display them in form, save them in JS variables...). Hope this helps.

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

1 Comment

Its a very good idea. i will try to implement with this approach and let you know the result.
0
javascript variable available in scriptlet

NO.

Inside javascript ,you cannot call jsp,The code with in the scriplets executes on serverside

You need to either make a request to server(HTML forms/AJAX) for new content or Maintain Json object in client side itself while page loading and use it later.

Comments

0

If your intention is to parse the JSON string, you can do it on the client side as well.. you can use jQuery.parseJSON (http://api.jquery.com/jQuery.parseJSON/) or eval (eval is not recommended though)

1 Comment

Actually i have to create my json object in server side which will create a jquery jstree. this object data is coming from some other api.
0

You can send a request to the same resource (the JSP page) the first time the page is loaded, so the scriptlet can use the javascript variable.

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.