JavaServer Pages is quite similar in purpose to servlet code.However the big difference is that JSP code is embedded inside a HTML web page rather than executed as a separate program.So this exercise involves creating a HTML web page with someembedded Java code.
Level of Difficulty: 1 (easy)
Estimated time: 15 minutes
Pre-requisites:
This fairly trivial introduction lab will get you started with creating yourfirst JSP in Eclipse.
Use Right Mouse Button -> New -> JSP File to create a file called HelloWorld.jsp.
Note that the "New JSP File" Wizard automatically highlights theWebContent directory. This is the root of your web application.
Click "Next" and choose the JSP file (html) template which should open an editor window with a skeletal JSP (much like the static html exercise).
Edit the file to match the following contents:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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=UTF-8"><title>My First JSP page</title></head><body><h1>My first JSP page</h1><p><% out.println("Hello JSP World"); %></p></body></html>
Note the following points:
The Java code is embedded between the tags <%
and %>
This is called a JSP scriptlet.
There is an implicit object called out
available in every JSP page that you can use without pre-declaring. Think of it as a "built-in" object. Servlets that you created also used an out
object, but in a servlet, you had to explicitly initialise it, e.g.
PrintWriter out = request.getWriter(); out.println("Hello Servlet World");
In a JSP page, you can use the out
object without needing to initialise it, as an instance is implicitly always available in every JSP page. (Note: the out
object inserted by the JSP compiler is actually of type JspWriter, which is a JSP-specific class that emulates the features of PrintWriter while supressing IOExceptions.)
You do not need to compile your JSP page.
Run your new JSP by doing the usual Right Mouse Button, Run As -> Run On server.
Once your server is running, you can also view the JSP in a web browser, by viewing a URL likethe following.
Note that the first part of the URL path is the context root name (in our case, labs), but this can also be the WAR filename if you exported the project as a WAR file and copied it to the autodeploy directory.
http://localhost:7001/labs/HelloWorld.jsp
When the web server loads your JSP file, there will be a delay of up toa few seconds. What is actually happening is that the JSP file you createdis being compiled automatically into a servlet (yes, a servlet!). Theweb server will then execute the servlet it has generated from your JSP,and keep a cached copy of the generated class file so that it doesn'tneed to recompile the JSP on subsequent requests.
So although there is a delay, it only happens once (or rather, only onceeach time you modify the JSP file).
Let's call this images
Now you can create more JSP or HTML files into this directory, but for this example, we will demonstrate how to import files from the filesystem.
Select the images folder and click Right Mouse Button -> Import
Expand General, then choose -> File System
You can then type in a file name or use the Browse button to find one. Let's browse to /pub/aip/images and select yellowstar.gif
You can now see this file in your WebContent/images directory and can refer to this in your JSP
Modify your JSP to reference this image ie: add the line
<img src="images/yellowstar.gif">
(note that we do not put a / in front of images. This is because the file must be relative to the context ie: labs)