Overview of Java Web Technologies, Part 2 (Page 1 of 10 )
Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers JavaServer Pages (JSP), with a thorough overview of JavaBeans and Tags. This excerpt comes from chapter one of
JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).
JavaServer Pages (JSP)
Sun introduced servlets in 1996, and this technology soon became popular as a faster solution than the Common Gateway Interface (CGI) technology, which was the first technology for writing Web applications. However, Sun realized that writing servlets could be very cumbersome, especially if you need to send a long HTML page with little code. Take the following servlet as an example:
import javax
.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class MyLongServlet extends HttpServlet {
//Process the HTTP GET request
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
//Process the HTTP POST request
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Using Servlets</TITLE></HEAD>");
out.println("<BODY BGCOLOR=#123123>");
//Get parameter names
Enumeration parameters = request.getParameterNames();
String param = null;
while (parameters.hasMoreElements()) {
param = (String) parameters.nextElement();
out.println(param + ":" + request.getParameter(param) +
"<BR>");
}
out.println("</BODY>");
out.println("</HTML>");
out.close();
} //End of doPost method
} //End of class
Half of the content sent from the
doPost method is static HTML. However, each HTML tag must be embedded in a String and sent using the
println method of the
PrintWriter object. It is a tedious chore. Worse still, every single change, even the change to a color code in an HTML tag, requires you to recompile the servlet.
Remember: This is part two of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more chapters of developer books from McGraw-Hill/Osborne. Buy this book! |
Next: Sun's Solution >>
More Java Articles
More By McGraw-Hill/Osborne