02162: Course on Software Engineering 2 (e14)
In this tutorial, you will learn how to install and use the
Eclipse IDE for Java EE Developers (Eclipse WTP) for developing
and running simple web applications on a web server (GlassFish).
Up to now, the web application will consist of simple
dynamic web pages (JSP and Servlets) only. Later, there will
be tutorials for implementing web services and database support
(using Derby and Jersey).
First, you will install the Eclipse IDE for Java EE Developers
and the GlassFish server;
then you will develop a first web application. The steps are explained
below. The main steps will also be discussed in the tutorial on
Friday, Sept. 5. The tasks should be finished by Sept. 12. Open questions
or problems will also be discussed in the tutorial on Sept. 12.
Generally, it is enough, if one or two members of each group complete the
assignment. But, all students should install the Eclipse WTP and the
GlassFish server and have a rough understanding of web projects.
Note that the groups do not need to submit their solutions now (even though
they should be completed by the given deadlines). All the completed tutorials
are to be presented by each group in the "Tutorial presentation" in week 45
(see material and schedule page).
Tasks
Here are the tasks for the first assignment with step by step instructions.
- If you do not have JDK7 installed on your computer install the JDK7. You will find
the instructions and necessary downloads here.
- Next, you need to install Eclipse WTP with GlassFish Tools and the GlassFish4 server.
To this end, follow the following steps.
- Download the Eclipse IDE for Java EE Developers (Eclipse WTP) for Eclipse luna (4.4)
for your platform and install it on your computer. For Windows, it is recommended to
use the 64-bit version (eclipse-jee-luna-R-win32-x86_64.zip). Extract the file to
some appropriate folder on your computer.
Once extracted, there should be a folder "eclipse" that contains an executable file
also called eclipse (eclipse.exe on Windows). You can start this installation of Eclipse
by executing this file.
- Start this new installation of Eclipse as explained above. On the first startup, you
will be promted for a folder in which the workspace of Eclipse should be stored. You
can choose any folder you like; I suggest to create a new folder with a subfolder
called "ws"; on Windows, make sure that the path is short!
- Once this new Eclipse is running, install the GlassFishTools for Eclipse feature to
it. You can do this with Eclipse's install dialog (Help -> Install new Software).
In the opended "Install" dialog, first add the following new update site:
http://download.oracle.com/otn_software/oepe/luna/.
To this end, press the "Add" button and enter the above URL and some name.
Then, select this update site in the "Work with" drop down menu, enter GlassFish
in the filter field below. Then, the "GlassFish Tools" feature should show up.
Select this "GlassFish Tools" feature and follow through the installation dialog
(note that you will be asked for accepting the license). Once the installation
process is finished, restart Eclipse.
- Before you can start web applications on GlassFish, you need to install GlassFish
first. To this end, download the platform independet version of GlassFish4 from
https://glassfish.java.net/getstarted.html
(you can click here
for downloading the glassfish-4.0.zip file directly).
Unzip this file to some folder which you find appropriate (remeber the path — you will
need it in the next step).
- At last, you need to register the GlassFish server with your Eclipse WTP, so that Eclipse
can directly deploy and start your Web applications on the GlassFish sever.
Before you do that, make sure that you have selected JDK7 as your default runtime environment.
You can do that in your Eclipse preference (Window->Preferences->Java->Installed JREs).
Normally the "Servers" view should be open in your Eclipse workbench. If it is not, open
this view via Window->Show View->Other...->Server->Servers. Once you opened this
view (normally, it opens at the bottom of the Eclipse workbench), right-click in it and
select "New->Server". Then, in the "New Server Dialog" window select "GlassFish4" in
category "GlassFish" and follow through the process; in the dialog "New GlassFish 4.0 Runtime",
select the "Browse" button for selecting the directory where you installed your
GlassFish4 server (the folder "glassfish" from yor GlassFish4 installation
from above). Note that do not need to configure the resources for this server, since you do not
have any web projects yet.
- Now, the installation of Eclipse WTP with GlassFish4 should be complete. To test it, you need to
create a simple web project and deploy it. This is done in the steps below.
- First, create a new web project by File->New->Dynamic Web Project and following through
the dialogs (make sure that you create a "dynamic" web project").
Normally, this wizard should create a file "index.jsp" in the folder "WebContent". If such
a file is not there you can use this example file instead;
make sure to copy it to the folder "WebContent" (and to name it "index.jsp").
- Now start the GlassFish4 server with this project. You can do this from your Eclipse workbench:
Select the project in the Eclipse Project Explorer, right-click on it and choose
Run As->Run on Server from the pop up menu.
Once successfully started, Eclipse also starts a web browser on the homepage of this project.
It will show the content of your index.jsp.
- The contents of the above index.jsp is a bit boring (it is just static content). In order to
make it more dynamic, you could add some content that is dynamic, such as showing the
current date, greeting the user by "good moring" or "good evening" depending on the
current time etc. The simplest way of doing this is puting some code like
<%= new java.util.Date() %> somewhere to your JSP.
Check whether your new index.jsp works dynamically now by restarting the web application
in the same way as discussed above.
Actually JSP is much more powerful than that, which we cannot explain here in full (see
links at the bottom of this page for some more details).
- Now, add a HTML from (see for exmple http://www.tutorialspoint.com/html/html_forms.htm) to your index.jsp (in the version provide
above, there is a form already). This form
should have an input field for the user's name and a submit button. By using an empty
action and the POST method the user's name is posted to the same page again. Then, make sure
that the index.jsp greets the user by name, if the user has entered a name.
The value of a posted parameter can be accessed by request.getParameter("yourname")
in case the input field had the name "yourname".
Restart your simple web application and make sure that it works as intended.
- At last, you should try to do the same thing with servlets. Just put a servlet class into
a package of your "src" folder in the "Java Resources" folder. First create a new package
in the "src" folder and then create a new servlet by New->Servlet. If you want, you
can use the example from https://glassfishplugins.java.net/nonav/eclipse36/MyServlet.java to start with.
Next change this servlet so that it has the same functionality as your index.jsp from above.
Note that, in a servlet, you can access the value of a posted parameter by
request.getParameter("yourname") , too (if request is the name of the
request parameter of the respective doGet() or doPost() method.
Test also this servlet of your application. The servlet can accessed in the running web
application on the GlassFish server by adding the name of the servlet to the URL of your
web application running on GlassFish.
- Finally, read up a bit more on JSP and Servlets (see links below), and also on the
possibilities of the Eclipse WTP IDE (you can do all kinds of things, such as
debugging your web application). In particular have a look a the following
web page: https://glassfishplugins.java.net/nonav/eclipse36/ — for now, we
do not use the database, though. Note that this tutorial referes to older versions
of Eclipse and GlassFish; most features, however, are still the same (for the
installation, you find the correct URLs, downloads and versions for GlassFish4 and
Eclipse luna in the instructions above).
Useful links
Here are some links that might be helpful (some of which are mentioned in the above
instructions already).
|