• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets don't generate HTML

 
Doug Gschwind
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
paul
 
Doug Gschwind
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that does help, and makes good sense. I was wondering if that approach was possible.
Thanks for the help.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also the discussion at http://www.javaranch.com/ubb/Forum7/HTML/000628.html which covers this topic in a lot of depth.
 
Joe Angott
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic