Results 1 to 7 of 7
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question Is reading HTML code and printing on JSP pratical for web development with Java?

    The idea:
    Grab a template HTML, read it on the JSP file and replace a few code portions and print the modified code to the user.

    The question:
    Picturing that every page on the website were to be done like this, does this wastes too much resource from the server? Is this a pratical way i could do this?

    The code (example):
    Java Code:
        public void loadTemplate(String template) throws IOException {
            String line = null;
            String f = this.filePath(template);
             
            BufferedReader bfr = new BufferedReader(new FileReader(f));
            while ((line = bfr.readLine())!=null){
                this.tplCode += line+'\n';
            }
            this.tplCodeModified = this.tplCode;
            
            // Do stuff here and replace w/e code you wanted from this.tplCodeModified
    
            bfr.close();
            
    out.print(this.tplCodeModified)
        }
    I already got this working, but my question is if this is gonna scale well on live servlet. Since i'm a newbie on Java i don't really know if this is good practice.

    Thanks in advance!
    Last edited by KingdomX; 01-18-2012 at 02:58 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Is reading HTML code and printing on JSP pratical for web development with Java?

    I recommend reading about the Composite View design pattern. For instance:
    Design Patterns: Composite View

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Is reading HTML code and printing on JSP pratical for web development with Java?

    Grab a template HTML, read it on the JSP file and replace a few code portions and print the modified code to the user.
    For what purpose? Also, what do you mean by "read it on the JSP file" ?

  4. #4
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Is reading HTML code and printing on JSP pratical for web development with Java?

    The purpose is to create a framework. I got the framework already working the way i want it too, but i don't know if reading the file is a viable way to do a website resource wise, opposing to the other frameworks around like Struts, etc. I don't really know if these frameworks also read files and replace code or if they work in a different way... i've been searching for a while now on these frameworks' mechanics, but i couldn't find anything on the subject.

    The reading on the JSP file means that the JSP file would grab a HTML file (template), read it line by line and add these lines to a string. This string is then modified when necessary and, in the end, it's printed to the user displaying the built website.

    My question is if this will waste resources around opposing to using other frameworks?

    By the way doWhile, thanks for that link. Very important reading indeed... i suppose i'll have to implement that too. Thanks!

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Is reading HTML code and printing on JSP pratical for web development with Java?

    My question is if this will waste resources around opposing to using other frameworks?
    Probably. Remember that mature frameworks like springMVC, etc... are mature for a reason.

    That being said, would it be enough extra overhead to matter? Probably not. You'll definitely want to use StringBuilder instead of raw concats though, it is substantially faster!

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Is reading HTML code and printing on JSP pratical for web development with Java?

    Did you read the link I provided? This is a common design pattern used in web development - and you don't need to read the file, you include it and it will be parsed inline with the rest

  7. #7
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Default Re: Is reading HTML code and printing on JSP pratical for web development with Java?

    Quote Originally Posted by quad64bit View Post
    You'll definitely want to use StringBuilder instead of raw concats though, it is substantially faster!
    Nice! Thanks a lot for that... really good info! Indeed i'll have to use that.

    Quote Originally Posted by doWhile View Post
    Did you read the link I provided? This is a common design pattern used in web development - and you don't need to read the file, you include it and it will be parsed inline with the rest
    Yes i did read it! But i'll have to do some studying on that subject. Thanks for the info!

    Thanks a lot for input guys!

Similar Threads

  1. Replies: 8
    Last Post: 06-06-2011, 02:27 AM
  2. I need some java / html code written
    By DeDanan in forum New To Java
    Replies: 2
    Last Post: 03-01-2011, 09:36 AM
  3. Is this the right way of writing html code in java??
    By raju.i in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2010, 08:05 AM
  4. Help in reading values from html form in java
    By ichkoguy in forum Advanced Java
    Replies: 7
    Last Post: 03-16-2009, 07:45 AM
  5. J2EE, PHP, Linux, MySql, CSS, HTML Development Professional
    By softdev in forum Reviews / Advertising
    Replies: 1
    Last Post: 01-17-2008, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •