Ineffecient Servlet Programming , Help to improve!!  
Author Message
cheung_yuklun





PostPosted: 2003-8-21 17:24:00 Top

java-programmer, Ineffecient Servlet Programming , Help to improve!! Dear all,

I have a query on servlet programming.

When I write html syntax via println, such as

String msg = "hello";
PrintWriter writer = resp.getWriter();
writer.println("<html><body>" + msg + "</body></html>");
..

I found it is quite trouble on typing the html tags.
If my output html is complicated, I need to type or retype many html tag.

Can any one suggest me a good method to improve it?
or any example website? or sample?

Thank you very much.
Best regards,
Alan
 
gfxguy





PostPosted: 2003-8-21 22:40:00 Top

java-programmer >> Ineffecient Servlet Programming , Help to improve!! Have you tried using a combination of JSP and Servlets? Ideally, most
of the HTML code goes on the JSP page, which lets you worry about
formatting from the perspective of the HTML writer instead of the Java
programmer.

It's not perfect, but you should be able to move most of your HTML out
of the Servlet, which allows you to quickly modify the appearance of
your pages without having to recompile (which often means restarting
your server).

In the MVC design, this allows JSP pages to be the view. I didn't get
MVC at first (and I'm always learning more), but the more I write web
applications, the more sense it makes, and the more I find I kick
myself when I break with the methodology.
 
Wendy S





PostPosted: 2003-8-22 8:34:00 Top

java-programmer >> Ineffecient Servlet Programming , Help to improve!! "David Rudder" <email***@***.com> wrote in message
> Then, the JSP looks like (this is file
> Hello <%(String)request.getAttribute("name")%>!!!<br>

Or even better, with JSTL:

Hello <c:out value="${name}"/>!!!<br>

And Tomcat 5 (Servlet 2.4??) the expressions will be evaluated wherever, so
you have just:

Hello ${name}!!!<br>

But definitely move the HTML stuff to a JSP and leave the Java code in the
Servlet, as much as possible. Look for some articles on "Model 2" or "MVC"
to help you understand how to separate the layers of your application. The
JSP shouldn't do much, it should just display information.

--
Wendy in Chandler, AZ