Frage

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).

War es hilfreich?

Lösung

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.

Andere Tipps

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.

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)

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top