Design Issue

I am half way through a project and I have a design issue. I need my Java program to generate a html page. I have designed a servlet that will do this for me. It basically takes input from a file and generates the html page. The html file is expected to be different each time as the input from the file is expected to be different each time. My code works but despite every effort the servlet file is a mess to read. This comes from the java code and the html code being interspersed. Basically I have blocks of html code and I substitute my content for tokens(place holders) in the html code. Firstly is this a reasonable approach? Secondly if i were to do it all again tomorrow should I switch to JSP pages? Is this what JSP pages were invented for - to separate html code from java code? I know nothing about them and won't have the time to learn them before the deadline. Any thoughts would be appreciated.

[919 byte] By [Robbie_Ca] at [2007-10-2 17:23:15]
# 1

If it works and you don't need to change is often nor expand it then it is fine. You can move the html to constant strings though rather than embedding it directly in your code.

There are many ways to handle this in general. Whether it is better depends on the situation. You could for instance use XSLT. Or given that it sounds like a report you could use Crystal Reports. Or you could load the html from file(s). Or load it from a database. Or generate a text file which is then used as a source file for another class that does nothing but format it.

jschella at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 2
> Is this what JSP pages were invented for - to separate html code from java code?JSP embeds the Java into the HTML rather than the other way round.
YAT_Archivista at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 3

> > Is this what JSP pages were invented for - to

> separate html code from java code?

>

> JSP embeds the Java into the HTML rather than the

> other way round.

Originally that was true. However modern uses of JSP deprecate the use of Java scriptlets and encourage the use of JSTL, which is yet another tag language.

DrClapa at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 4

>You can move the html to

> constant strings though rather than embedding it

> directly in your code.

That's what I believe I am doing. I assign html blocks to Strings, plug-in my stuff, then concat all the strings to get my html page. For increased clarity should the html blocks and the strings they are assigned to, be declared in a seperate source file?

Robbie_Ca at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 5

for performance reasons it's better to stream the strings directly to the client instead of concatting them all into a big string first and then send that big string to the client. This last method causes a major memory issue when the string becomes very large, and all the string concats are a major drain on CPU as well.

I used to do it that way, untill I had to build a web-page over 2k in size... then it takes about 5 mins for the page to load. After switching to the stream method, that time dropped to 2 secs.

ractoca at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 6
good tip. thanks.
Robbie_Ca at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 7

>

> I used to do it that way, untill I had to build a

> web-page over 2k in size... then it takes about 5

> mins for the page to load. After switching to the

> stream method, that time dropped to 2 secs.

2048 bytes?

Something was seriously wrong with your processing (code) or your network for it to take that long.

jschella at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 8
Not really, the actual download of the content took very little time, the most time was taken by the servlet in concatenating the strings.After removing that, the most time was taken by the browser in rendering the page.
ractoca at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...
# 9

> Not really, the actual download of the content took

> very little time, the most time was taken by the

> servlet in concatenating the strings.

> After removing that, the most time was taken by the

> browser in rendering the page.

Again something was seriously wrong with the processing.

If you concatenate 2048 separate characters it is going to take far less than 1 second.

jschella at 2007-7-13 18:39:27 > top of Java-index,Other Topics,Algorithms...