Ineffecient Servlet Programming , Help to improve!!

A

alan

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
 
F

Fred

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.
 
W

Wendy S

David Rudder said:
Then, the JSP looks like (this is file
Hello <%(String)request.getAttribute("name")%>!!!<br>

Or even better, with JSTL:

Hello <c:eek:ut 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top