Home > Articles

Getting Started

  • Print
  • + Share This
This chapter is from the book

Dissecting Our First JSP

The crucial line in our first JSP is this one, where it uses some Java code:

<HTML>
 <HEAD>
  <TITLE>A Web Page</TITLE>
 </HEAD>

 <BODY>
  <% out.println("Hello there!"); %>
 </BODY>
</HTML>

The special JSP tags <% and %> create a scriptlet, and we can enclose Java code in the scriplet. In this way, we're able to embed Java directly into our page's HTML.

The actual Java we're using here is out.println("Hello there!"); (note the semicolon—all Java statements end with a semicolon). Here, we're using the out object to print Hello there! to the Web page before that page is sent back to the browser.

A Java object like out contains all kinds of programming power for us. For example, here we're using the out object's println (which stands for "print line") method to write Hello there! to the Web page that is being sent to the browser. The methods of an object let you perform actions, such as writing text in a Web page. Besides methods, objects also support data members. You use data members to configure the object; for example, you can use the out.bufferSize data member of the out object to set the size of the internal buffer used in that object. You'll see more on objects, methods, and data members in Day 2, "Handling Data and Operators," and in detail in Day 6—all that's important right now is to know that in JSP, you can use the out object's println method to write text (including HTML!) to a Web page that is being sent back to the browser.

The out object is one of the objects already built into JSP for our use. Note that in our servlet example, we had to create this object ourselves, using techniques we'll look at later in this book:

public class servlet extends HttpServlet
{
  public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<HTML>");
    .
    .
    .

In JSP, the out object is already created for us in order to make things easier, and in this example, we're using the println method to write text to the Web page being sent back to the browser. That's all there is to it.

  • + Share This
  • 🔖 Save To Your Account