File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes question related to out.println( ) and HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "question related to out.println( ) and HTML" Watch "question related to out.println( ) and HTML" New topic

question related to out.println( ) and HTML

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
i have seen in many coding examples

PrintWriter pw = response.getWriter();
pw.println("<h3>Hello World!</h3>");

is it not possible to combine all html related code in single out.println()
Mishra Anshu
Ranch Hand

Joined: Sep 16, 2003
Posts: 224
If you are not making the html generated through the JSP (as compared with Servlets), it is better to use StringBuffer of the related Html code. And just use pw.println(theStringBuffer).

"Ignorance is bliss"
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

It's certainly possible but it's usually more clear if you do it this way - especially if you have a lot of HTML. It also makes it easier to add and remove lines without breaking the code.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
And, if it is HTML that you are trying to return to the client, then perhaps a JSP is the better vehicle. It will save you from repeatedly typing in the println() statements.

Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Sharad makes a good point. One very common, and good, pattern is to let the servlet perform all the Java processing, place whatever data is relevant onto the request as scoped variables, and then forward to a JSP page to render the view.

This takes advantage of the best of both worlds: Java processing is best performed in Java classes (rather than in a JSP), and HTML generation is much easier within a JSP.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: question related to out.println( ) and HTML
It's not a secret anymore!