Implementing your very first JSP using Eclipse WTP

The previous sections got us started with Tomcat, servlets, and Eclipse WTP. As you saw, despite the benefits provided by Eclipse WTP (it generated all the structure of the web project for us and also took care of generating a significant chunk of code when we implemented the previous example servlet), embedding HTML into your Java code feels a little awkward. As we mentioned, JSPs were introduced to make the creation of dynamic content more straightforward and appealing. JSPs let you mix HTML and Java code in a better way than servlets. Rather than embedding HTML into Java code, JSP makes it possible to embed dynamic content into HTML. Recall from the previous sections that a JSP is an HTML page containing some special elements. The elements that are rendered into dynamic content resemble HTML elements. However, they are componentized Java programs. Besides these HTML-like elements, a JSP page can also include Java code (scriptlets).

It bears reminding you of the fact that although JSPs look like standard HTML pages with additional elements, they are compiled down to servlets. As we mentioned, at runtime, Tomcat compiles JSP pages down to servlets, compiles the servlets to bytecodes, and executes them eventually.

In this section we will demonstrate how to use Eclipse WTP to create JSPs. We will implement a JSP that shows the same message of the servlet example.

You do not need to create a new web application project; we will use the previous one. To get Eclipse to create a new JSP file for you, do the following: select the folder WebContent (Tomcat will look for JSPs in this folder), and right-click on JSP File in New. Name it HelloWorldJSP.jsp (as shown in the following screenshot), and then hit Next. Actually, you do not need to type the extension (.jsp), Eclipse will include it automatically:

Implementing your very first JSP using Eclipse WTP

Afterwards, select the option named New JSP File (html), whose description is JSP with html markup (as illustrated in the following screenshot), and click on Finish:

Implementing your very first JSP using Eclipse WTP

Eclipse will generate the following code. The first line is a JSP directive element. Directive elements specify various attributes related to the page itself. Thus, it does not necessarily change the response sent to the browser, but it informs the container how it should deal with the page. The first line is a page directive that, among other things, specifies the content type for the page in question: html/text. JSP directive elements start with <%@ and end with %>, and between these two delimiters a list of name/value pairs represents the so-called directive attributes.

Apart from the page directive, the auto-generated code contains mostly HTML code. In JSP parlance, everything that is not a JSP element is named template text. Template text is static, which means that it is sent to the browser without undergoing any transformation.

As for the template text in the auto-generated code, there is a title placeholder and the body part is empty. If you know HTML and Java, writing JSP pages should be a breeze for you. If you are not that familiar with HTML, creating JSP pages may be a little tricky at first.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
Let's start by replacing the title placeholder by the title we used in the previous example, namely, "Warm Greeting":
<title>Warm Greeting</title>

Next, let's add the Hello, World! static message as well as the dynamic part of the message: the timestamp. Adding the former is straightforward, just copy part of the HTML code shown in the example servlet. To include the latter, we are going to embed a Java expression in our page by putting them between <%= and %> as shown in the following code snippet:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Warm Greeting</title>
</head>
<body>
<h1> Hello, World! </h1> <%-- title as template text --%>
<%-- timestamp as embedded expression --%>
<ul> <li>current time: <%= new java.util.Date() %> </li></ul>  
</body>
</html>

If you run the preceding example, you will get exactly the same message that you got when you executed the example servlet. Now, let's go over the most important parts of the chunk of the preceding code. First, as you might have noticed, everything placed within <%-- and --%> is ignored by the container. You can use this to describe what is going on in your code or to comment out parts of the code. Given that JSP comments are also JSP elements, they never make it to the browser. Second, instead of concatenating a string and using plain I/O to write to the response, the preceding code uses a scripting element through which you can add Java code to JSPs. When you want to simply add Java code without writing the result to the response, you use <% and %>. When the result needs to be added to the response, you use <%= and %>. Thus, we used the latter to write the timestamp to the response. The next example shows yet another way to achieve the same thing, but this time using more JSP elements and scriptlets. Note that the page directive is also used to import java classes (and packages), as shown in the following example. In addition, when importing more than one class, the list of fully-qualified names must be separated by commas. After adding import instructions to the page directive, you do not need to use the fully qualified class name inside scriptlets.

Tip

You can also print stuff within a <% and %> pair. To do so, you have to use the out object. This object is always implicitly there. For example, you could print the Hello, World! message as: <% out.println("Hello, World!") %>. Nevertheless, it bears mentioning that using out.println() inside a scriptlet is a bad practice. Whenever possible, use an expression (<%= and %>) instead.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1" import="java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Warm Greeting</title>
</head>
<body>
<h1> <% out.println("Hello, World!"); %> </h1>
<% Date date = new Date(); %>
<ul> <li>current time: <%= date %> </li></ul> 
</body>
</html>

Tip

Never end expressions with a semicolon. <%= date %> is correct, <%= date; %> is not.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset