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):
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!
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
Re: Is reading HTML code and printing on JSP pratical for web development with Java?
Quote:
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" ?
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!
Re: Is reading HTML code and printing on JSP pratical for web development with Java?
Quote:
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!
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
Re: Is reading HTML code and printing on JSP pratical for web development with Java?
Quote:
Originally Posted by
quad64bit
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
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!