First lab
Part 1 - install and start GlassFish
Find your PC's host name
Run the "hostname" command at a Windows
command prompt
Write down your hostname
Download and install GlassFish
Follow the instructions in the
installing-GlassFish page
When you finish this process, the menu
service will be up and running on your PC
Try the menu service on others' PCs
Create the base URLs using the hostnames of anohter student's computer in the lab
Part 2 - developing a trivial Servlet
Start Eclipse
you may need the Eclipse IDE for Java EE Developers package for this course. Go to
http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/heliossr2 to download it if it is not on your computer.
Create a dynamic web project
File > New > Project... > Web/Dynamic Web Project
Name the project "tiny"
I expect that you can accept default settings. We will
confirm this in the lab. When asked if you want to open the J2EE perspective,
click Yes. "J2EE" should appear in a highlighted box in the upper
right of the Eclipse window.
Create a servlet
In the J2EE perspective, right-click your new project
and pick New > Servlet
Use tiny
as your Java package name
Use TinyServlet
as your class name
Accept defaults for the other options
TinyServlet.java should appear in an editing window in
Eclipse, with all of the standard classes such as HttpSevletRequest
highlighted as un-resolvable. To fix this, do the following
Notes about this:
- In some Eclipse installations, if you pick Build Path >
Configure Build Path in the J2EE perspective, nothing happens. If that's
the case, switch to the Java perspective, by clicking the "Java"
button in the upper right. If no "Java" button is available in
the upper right, pick Window > Open Perspective > Java.
- If you switch to the Java perspective, right-click
your project, and find no Build Path option in the menu, you're probably
not in the Package Explorer. Pick Window > Show View > Package
Explorer, and perform the steps above in the Package Explorer.
- If you switch to the Java perspective to configure
your build path, switch back to the J2EE perspective for the rest of this
procedure.
Implement minimal GET operation
Use the following code for your doGet()
implementation
String name =
request.getParameter("name");
if( name == null ||
name.length() == 0 ) name = "no name given";
response.setContentType("text/xml;charset=utf-8");
response.getWriter().println("<name>"
+ name + "</name>");
Export your project as a WAR
Right click your project and pick Export > WAR file
For the destination, navigate to C:\glassfish3\glassfish\domains\domain1\autodeploy
Check "Export source files" and
"Overwrite existing file"
Click Finish
Try your servlet
Point your browser to http://localhost:8080/tiny/TinyServlet?name=whatever
Or, you can use the RESTful
client, which should already be running, or if it's not running, it should
be available at http://localhost:8080/restful-client.
Select "tiny" in the restful-client's base URL
menu, then click the GET button
Try the admin console
Point your browser to http://localhost:4848
Login as admin. The password is "adminadmin"
Click on Web Applications under Common Tasks in the left-hand navigation area. You should see your
"tiny" application.
Part 3 - JAX-RS (time permitting)
Open menu.war with
WinZip and look at the source files in WEB-INF/classes
Or, extract the files from the war file and look at
the source files in WEB-INF/classes
Or, if you are comfortable with Eclipse, you can
- create a dynamic web project
- import the menu package from menu.war
- import the web.xml from menu.war
- import the jar files from menu.war to WebContent/WEB-INF/lib
I will give a short introduction to JAX-RS
and (a small piece of) JAXB, using the menu service source as an example.
|