When Java first came to the Web, you used Java servlets to create interactive web pages. Servlets are compiled Java code, and they take a little bit of practice to write. For example, here's a servlet that displays the text "Hello there!" in an HTML page: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Sample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>"); out.println("A Web Page"); out.println("</TITLE>"); out.println("</HEAD>"); out.println("Hello there!"); out.println("</BODY>"); out.println("</HTML>"); } } If you want to use servlets like this, you need to compile them, making sure that the class support for servlets, bundled into a JAR file, is in your class path. Also, you have to edit a deployment descriptor file when you install a servlet on a web server. It's much easier to use JSP instead of servlets for this project, and that's how Chat is writteneverything revolves around chat.jsp. Here's a sample JSP file that does the same thing as the previous servlet didit displays the text "Hello there!" in an HTML page: <HTML> <HEAD> <TITLE>A Web Page</TITLE> </HEAD> <BODY> <% out.println("Hello there!"); %> </BODY> </HTML> As you can see, this is shorter, and it's all HTML except for one line, which is enclosed in the markup <% and %>. That's a JSP scriptlet, and in this case, it's executing this single line of Java: out.println("Hello there!"); This line of code uses the out object, which is set up for you by the JSP framework, in order to display text in the browser. Just as you can use System.out.println to display text in a console, you can use out.println in JSP code to display text in the browser. Here, however, the out object is a JspWriter object. This is a lot easier than working with servlets, where you have to instantiate your own objects before working with them. For example, if you want to write to the browser in a servlet, you have to create your own out object, which you can do like this: public class Sample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<HEAD>"); . . . Note also that when you want to send HTML back to the browser, as in the last two lines in the preceding code snippet, you have to use Java to do it. In a JSP page, on the other hand, you can mix simple HTML with Java in the same documentwhen you want to use Java, you just enclose your Java code with the markup <% and %>, making it a JSP scriptlet.
NOTE Make no mistakeeven though JSP is easy to write, you can use any Java code in JSP that you could in a servlet, and almost any that you can run on your desktop (with the exception, obviously, of windowing code). However, as your code gets longer, all that HTML mixed in with your Java gets annoying, and you should consider converting to servlets. The chat.jsp file starts with another of the built-in objects in JSP, the request object. This is the object that passes on the data from the web page to the server, and you can use that object's getParameter method to recover that data. For example, here's how chat.jsp starts, by recovering the parameter named t and testing whether it holds "1": <% if(request.getParameter("t") != null && request.getParameter("t").equals("1")){ %> <HTML> <HEAD> . . . To develop and run chat.jsp, you'll need a web server that can run JSP pages, and for this and upcoming projects, this book uses the Apache Tomcat server because it's easy to get and easy to run. Using this server, you can develop chat.jsp step by step. |