my dog learned polymorphism
The moose likes Servlets and the fly likes Servlets don't generate HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets don Watch "Servlets don New topic

Servlets don't generate HTML

Doug Gschwind

Joined: Sep 17, 2000
Posts: 27
I certainly understand and agree with placing all the presentation specifics in a JSP, leaving the fill in the blank info as the responsibility of the Servlet/backend.
Suppose you have the situation where the result of an end user's actions is a variable length (in terms of number of rows) HTML table. How can the JSP/Servlet be designed so that the Servlet (or elsewhere in the non-presentation logic) doesn't generate HTML? Seems like in this case the Servlet needs to render the table row, table data, and end table row constructs for each row.
Paul Wetzel
Ranch Hand

Joined: Nov 02, 1999
Posts: 107
The jsp can make a call to some class/object that may return a variable length list (lets just say a vector). the jsp code might look like this

so now nobody really needs to know how long the vector is (unless of course the vector is null but lets assume it will always return at size>=1). This is a pretty simple example but the process and reasoning are still valid for more complex examples where what is displayed is a result set from SQL.
hope this helps
Doug Gschwind

Joined: Sep 17, 2000
Posts: 27
Yes, that does help, and makes good sense. I was wondering if that approach was possible.
Thanks for the help.
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
See also the discussion at which covers this topic in a lot of depth.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Joe Angott

Joined: Oct 13, 2000
Posts: 7
You could also use the java code in your jsp to test (if lenth > 0 or !null) for the presence of data and the enclose the HTML display portion pertinent to the data in curly braces. This way the code in the table tags only gets seen by the browser when the test is true.
I agree. Here's the link:
subject: Servlets don't generate HTML
It's not a secret anymore!