#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    853
    Rep Power
    11

    Java Classes to Javascript Object


    I have the following class hierarchy

    Abstract class
    Code:
    public abstract class AbstractClass
    {
    	private long timestamp;
    	private String candidateId;
    	private String regId;
    
    }
    And I have multiple classes extending above abstract class as below in my java project
    Code:
    public class QuestionClass extends AbstractClass
    {
        
        private boolean questionanswered;
       private boolean valid;
    
        public QuestionClass(boolean questionanswered)
        {
            this.questionanswered = questionanswered;
        }
    
        public boolean isQuestionanswered()
        {
            return questionanswered;
        }
    
    }
    Now, I need to send from the browser the similar javascript objects(as json) to server (and on server I convert json to java object)

    For instance:

    {"candidateid":"1243","regId":"78","valid":"true","timestamp":"0","questionanswered":"true"}

    Is there anyway to dynamically create the javascript objects from java and get all the parent classes fields too
    If I can get javascript object from server java, then I can directly convert it to JSON while sending it back to server
    Right now, I have to manually code the similar javascript object

    Thanks
    Contact info:
    Primary email: advanced.programmer@gmail.com
    MSN/email: superprg@hotmail.com
    AIM: superprg
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    853
    Rep Power
    11
    Anyone?
    Contact info:
    Primary email: advanced.programmer@gmail.com
    MSN/email: superprg@hotmail.com
    AIM: superprg
  4. #3
  5. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    There are solutions to your question but this seems to be a very peculiar way of going about things. Why are you using this method to deploy Javascript?
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    853
    Rep Power
    11
    The reason is as I mentioned, I am using the same object at javascript end to monitor and get my related data in browser
    Contact info:
    Primary email: advanced.programmer@gmail.com
    MSN/email: superprg@hotmail.com
    AIM: superprg
  8. #5
  9. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    I understand why you are using the same Javascript as Java. I am asking why you are deploying it via a JSON object. To put it another way, why are you not simply printing the Javascript into the document?
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    853
    Rep Power
    11
    Sorry for the late response because of Sandy and other things..
    Let me explain more
    I do have the javascript objects in the client code(which I created by manually making it similar to the java classes)
    Json is coming in picture only while sending the data from browser to server(I can send in xml or json format and I preferred json since at server it was easy to convert it to java object)
    My question is, given 80 such java classes, is there any way to convert these classes to javascript objects?(More importantly, it would be good if this is possible at runtime too)
    Contact info:
    Primary email: advanced.programmer@gmail.com
    MSN/email: superprg@hotmail.com
    AIM: superprg
  12. #7
  13. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    Ok, I'll start off by saying that yes you can execute data as code by using the eval function. It is however extremely dangerous to allow that level of functionality on a client-side application.

    Another option is to create a file with the generated Javascript in and then insert the file into the document, as an active Javascript source.

    I'm still not clear on this. Is the code dynamic and therefore requires generation? Or is it only the data that is dynamic? If it is only the data then there is no need to send over the code also, at least not after the document has loaded.

    Can you explain the flow of this application and the requirement for transferring functionality after document load?
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.

IMN logo majestic logo threadwatch logo seochat tools logo