简体   繁体   中英

What is the best practice for outputing HTML from a Servlet?

I have a web application that uses a combination of JSP and Java Servlets. I use the JSP for the main form and all servlets for outputing the results.

I have used various techiniques for outputing the HTML show below, but they all seem a little rough and I was wondering what the best practice for this is?

Example1:

out.println("<html>");
out.println("<head>");
out.println("</head>");
...

Example 2:

out.println("<html> <head> </head> <body>") ...

Example 3:

String header = "<html> <head> </head> <body>";
output.println("<div> <p>" + result + "</p> </div>" );

Example 4:

Forward back to JSP for HTML and post result variable.

JSP is the view. Servlet is the controller. HTML is to be produced by the view. So, HTML belongs in JSP, not in Servlet.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM