Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

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:


A HelloWorld JSP

This fairly trivial introduction lab will get you started with creating yourfirst JSP in Eclipse.

  1. Open your labs project in Eclipse.
  2. 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.

  3. 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:

  4. (changes are in italics)
    <%@ 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:

  5. 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).

  6. If you do any subsequent changes in Eclipse, the changes should be visible immediately in the browser. HOWEVER, if you had exported and deployed a WAR file, you will need to rebuild/export and redeploy the WAR file to reflect the changes.

Creating a subfolder (subdirectory)

  1. Create a subdirectory (subfolder or subcontext in web terminology) by selecting WebContent then pressing Right Mouse Button -> New -> Folder

    Let's call this images

  2. 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

  3. 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)