Ineffecient Servlet Programming , Help to improve!!

Discussion in 'Java' started by alan, Aug 21, 2003.

  1. alan

    alan Guest

    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
     
    alan, Aug 21, 2003
    #1
    1. Advertising

  2. alan

    Fred Guest

    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.
     
    Fred, Aug 21, 2003
    #2
    1. Advertising

  3. alan

    Wendy S Guest

    "David Rudder" <> wrote in message
    > 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.

    --
    Wendy in Chandler, AZ
     
    Wendy S, Aug 22, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    2,045
    Jack Jia
    Apr 4, 2004
  2. ppcguy
    Replies:
    1
    Views:
    617
    Andrea Desole
    Aug 8, 2005
  3. Replies:
    10
    Views:
    556
    benben
    Feb 26, 2006
  4. Replies:
    0
    Views:
    304
  5. Jeff Rush
    Replies:
    0
    Views:
    364
    Jeff Rush
    Mar 9, 2007
Loading...

Share This Page