Creating JSP Pages


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.

REAL-WORLD SCENARIO

Why JSP?

There's a fair bit of programming in writing servlets, as you can see in the previous example. If you want to use an object like the out object to send text back to the browser, you have to create that object yourself and use its println method to send text. You have to compile the servlet, using various JAR files supplied by the servlet container (that is, the web server). You have to install the servlet and, typically, edit a deployment descriptor. Sometimes, the servlet container has to be restarted before the servlet becomes visible.

All that proved to be too much for casual HTML authors, and servlets were not a big success with the homepage crowd. Java programmers were fine with them, and still are, but the average homepage writer just didn't take to them.

That was a pity, because Java had been coming to the Web in a big way before that. In the early days of web development, web page authors loved Java appletssmall, embeddable segments of Java code that could display interactive graphics and controls such as buttons.

In time, though, applets became old hat, supplanted by newer options such as Flash and others. These days, some major browsers no longer even support applets.

So Sun turned to the server side of things. Servlets certainly worked, but proved too hard to master for many home developers. To make things easier, Sun introduced JSP.

JSP pages come with all kinds of objects ready for you to use, they don't need to be compiled, they don't need deployment descriptors, and you can mix HTML in with your Java code in the same pageno need to print everything out with Java methods.

For all these reasons, JSP has become very popular. Using JSP will also make your life easier when writing the Chat project. Internally, what happens is that the JSP page is translated into a servlet and run as a servlet. But you don't have to take care of any of the standard baggage that comes with developing servletsit's all done for you automatically by the JSP container, including compiling that servlet and running it.


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.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net