|
|
In the instructions below, substitute your UW Net ID wherever you see
"uwnetid".
Last updated: 23 Jan 2007
The development environment for web applications has improved considerably
since 2003, when you basically had to do everything yourself. This is true
of both the Microsoft Integrated Development Environment
(IDE -- Visual Studio) as well as the Java IDEs.
With the release of Visual Studio 2005, Microsoft led the way
with the tight integration of local web and database servers with the IDE.
Sun's Netbeans environment allows add-ons which provide a visual
means of specifying web applications, services, and service-oriented applications,
but may not tightly integrate web and database servers. This can be problematic,
as a much more complex development environment involving one or two servers
must be maintained. The open-source Eclipse project's
Web Tools Platform (WTP) does not bundle but allows connections to local web and
database servers, allows
debugging and provides a means of deploying applications to and
debugging them on remote servers
as well.
The configuration and operation of Eclipse with WTP for a simple
web application is described here.
An important point here is that there are several dynamic web "servers" (i.e.,
tomcat servers) that are possible:
- First, there is the tomcat runtime environment,
which is started and stopped within Eclipse as a "Server". It uses port 8080.
- The second is the tomcat server, which resides in
C:\Program Files\Apache Software Foundation\Tomcat 5.5. It also
uses port 8080 and would conflict with the Eclipse runtime version
if it was started. However, it does not start automatically -- one
starts it by entering the following
from a command shell (quotes are necessary):
"C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin\tomcat5.exe"
- The third dynamic web server you can use
is the one that you can start on
repos.insttech.washington.edu once you log into your repos account.
It uses a port other than 8080 -- one that is unique to your repos account.
It is considered to be a remote service, relative
to the computer on which you are running Eclipse. Since it is local to repos and
students were developing on repos in the past, it was called the
Local Tomcat Development Service.
But when you are developing your web applications and services on another
computer, it is a remote service to that computer.
All of these dynamic web servers require that the web application be deployed to
them. In a business setting,
this deployment often involves setting up some server-specific settings
for resolving names and properties (e.g., for
databases), but in most academic work deployment
means simply to transfer all code, web pages and XML files to the server.
Deployment happens automatically for the built-in tomcat server in Eclipse. For the
rest, you will have to do a little setup (the "second" server mentioned above)
or use systems already set up (e.g., the "third" server) to prepare it to
receive a deployed archive, create the web archive, and upload it to the server.
Installing the Tools
For instructions on installing most of the tools, see
Setup Eclipse with WTP.
Using Eclipse with WTP
The database service is not needed for this application.
- Start Eclipse
- Establish your Tomcat Server runtimes (one time only):
- Select Window/Preferences/Server/Installed Runtimes
- Click on the "Add..." button, and select "Apache/Tomcat 5.5 Runtime"
- Click Next
- Click Browse and browse to: C:\Program Files\Apache Software Foundation\Tomcat 5.5
- Select the correct JRE (in the labs, jdk) and click OK.
- Create a new Dynamic Web project
While not everything matches, you can use the information
in
this tutorial to cobble together a simple
web application. This tutorial is based on it, but gets the IP address
of the browser client -- the computer you are working on. There are
better ways of getting this, but it shows an interaction with the
browser and server. You may find this particular example useful
when testing a network, once this web application is deployed to
a remote server.
- Click on menu items File/New/Project...
- Expand Web
- Click on Dynamic Web Project
- Click Next
- Provide a project name.
For ease of use in later examples, call your project: ClientIP
- Click Finish
- Agree to any Sun license agreement.
- Click on Yes to the question of Open associated perspective?
- Optionally close the Welcome tab to provide more room to work.
- Expand ClientIP in the Project Explorer.
- Define your servlet class:
- Create a new package
- right click on Java Resources: src
- select New/Package
The package name can be anything you want it to be, but should
conform to standard package naming if you are going to use it elsewhere.
This is the reverse domain name of your organization (e.g.,
"insttech.washington.edu" is "edu.washington.insttech") followed by a
unique name within your organization (suggestion:
"edu.washington.insttech.uwnetid"). For example:
edu.washington.insttech.janedoe could uniquely identify
this project as Jane Doe's.
- Create a new Java class file
- right click on the package name (e.g., edu.washington.insttech.janedoe)
- select New/Class
We are going to start exploiting the "Content Assist" feature of Eclipse,
where Eclipse helps reduce the amount of typing you need to do and
details you need to remember. It can be activated either automatically
by typing a period, or manually by pressing Ctrl-Space, which we will
abbreviate to ~CS from now on.
To continue with the new file creation...
- enter the file's Name:
GetClientIP
- delete the old contents of the Superclass: field
- enter the Superclass:
javax.servlet.http.~CS
Don't forget that ~CS is just shorthand for Ctrl-Space
- select HttpServlet from the
presented choices with cursor, TAB and Enter keys, or
simply double-click on it
- click on Finish
- enter the body of the code -- the definition of a doGet()
method -- by using Content Assist:
- put cursor inside the public class code body
- press ~CS
- select doGet()
- set attribute for "client.ip" (arbitrary name) to the client
browser's remote address, as follows:
- Note that doGet's arg0 is the request, and arg1 is the response.
We don't change the names to something more meaningful to speed
up the creation of this web application. Normally, you would just
to make things clear.
- inside doGet() body, delete:
// TODO Auto-generated method stub
super.doGet(arg0, arg1);
- enter:
arg0.setA~CS
- select setAttribute()
- change highlighted setAttribute arg0 to the literal string:
"client.ip"
- press TAB to move to the next argument
- change highlighted setAttribute arg1 to:
arg0.getRem~CS
- select getRemoteAddress()
- append a semicolon to end of line
- specify .jsp file to run
- on the next line, enter:
arg0.getReq~CS
- select getRequestDispatcher()
- change getRequestDispatcher arg0 to:
"/show.jsp"
- enter a period at end of line (after right paren)
- enter (if necessary):
~CS
- select forward()
- accept both args (arg0 and arg1) as is -- press TAB twice
- enter a semicolon at end of line
- You should end up with code that looks like this (with your own
value for the package name):
package edu.washington.insttech.uwnetid;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetClientIP extends HttpServlet {
@Override
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
arg0.setAttribute("client.ip", arg0.getRemoteAddr());
arg0.getRequestDispatcher("/show.jsp").forward(arg0, arg1);
}
}
- save Java code
- change WebContent/WEB-INF/web.xml file to associate the
servlet code (the GetClientIP.class file) with a name and a URL
- right-click on the web.xml file
- select Open With
- select Text Editor
- delete all lines from <welcome-file-list>
to </welcome-file-list>, inclusive
- before the </web-app> line, add the following:
<servlet>
<servlet-name>Get Client IP</servlet-name>
<servlet-class>edu.washington.insttech.uwnetid.GetClientIP</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Get Client IP</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
- You should see the following in the final web.xml file, with your
unique package name instead of the generic one here:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>ClientIP</display-name>
<servlet>
<servlet-name>Get Client IP</servlet-name>
<servlet-class>edu.washington.insttech.uwnetid.GetClientIP</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Get Client IP</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- save the web.xml file
- create the show.jsp code
- right-click on WebContent
- select New/JSP
- enter Filename:
show.jsp
- click on Finish
- click in body of <body>
- enter:
My IP: <%= request.getA~CS
- select getAttribute()
- enter as its argument the literal string:
"client.ip"
- You should see this as the complete file - -if there is less,
that can be okay; all that is really required is the html and body
start and end tags, and the "My IP:" line.
<%@ 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>
My IP: <%= request.getAttribute("client.ip") %>
</body>
</html>
- save the show.jsp file
- select the server for the project
- right click on ClientIP project name
- select Run As
- select Run on Server
- select Tomcat 5.5 Server
- click on Finish
- verify the results
- If all goes well:
The built-in tomcat server will start, your web application will be
deployed to it automatically, and the built-in web browser will automatically
open to http://localhost:8080/ClientIP/, which
sends the request to start the web application to tomcat. The web application
executes, generating HTML code via show.jsp, and tomcat returns the HTML
code to the browser, which renders it as:
My IP: 127.0.0.1
- If all does NOT go well (e.g., you get an HTTP error or see "null" as
the IP address):
You probably have made a mistake typing something.
Carefully double-check your previous work, especially the strings that you entered,
including the ones that are unique to you (e.g., the package name).
Once you have fixed your problem, you must remember that the old
web application is still deployed on the server, which is running.
- Remove your project from the server:
- right-click on the server
- select Add and Remove Projects
- click on ClientIP
- click on Remove
- Stop the server
- Follow the instructions in the select the server for the project" item above.
- Re-verify the results.
- Set a breakpoint in the servlet (e.g., on arg0.setAttribute[...]).
- Start the Server in Debug mode -- it should restart, and show "Debugging" as
its status.
- Run the application by entering the URL in the browser -- the browser
should hang.
- Look at your Eclipse session -- it is probably prompting you to
"Confirm Perspective Switch", as it changes from a "Java" perspective
to a "Debugging" perspective.
- Look at variable values, step through code, and/or resume execution, all via
the Debug menu item choices.
- Eventually, the browser will either return with the expected results, or if you
stopped the debugging session, it may continue to hang until you click on the "Stop"
button.
Concisely:
- To deploy using repos:
- Start up the remote Tomcat service on repos with the debug option
- To debug using repos:
- Shutdown any existing remote Tomcat service on repos
- Start up the remote Tomcat service on repos with the debug option
- On your computer using Eclipse:
- Make sure your application is deployed
- Setup a Remote Java Application using repos.insttech.washington.edu and your
debug port number, apply it and click on Debug
- Set a breakpoint in your code
- Use a browser or web explorer with your URL to invoke your servlet.
- Handle the debugging perspective switch and debug your code.
For more detailed instructions,
follow this link to find out how to start your own copy of a remote tomcat
service on repos.insttech.washington.edu, as well as how to deploy
to it and debug an application running on that remote service from your
workstation.
This section is under construction and has not been well tested.
This section is adapted from a tutorial written by student Phil Bonderud
and
Developing Web Services "Eclipse Web Tools Project"
by Boris Minkin.
The database service is not needed for this application.
- Start Eclipse
- Establish your Tomcat Server runtimes (one time only):
- Select Window/Preferences/Server/Installed Runtimes
- Click on the "Add..." button, and select "Apache/Tomcat 5.5 Runtime"
- Click Next
- Click Browse and browse to: C:\Program Files\Apache Software Foundation\Tomcat 5.5
- Select the correct JRE (in the labs, jdk) and click OK.
- Create a new Dynamic Web project
The example code, instructions and explanations in
this web page
by Boris Minkin are very helpful:
Follow his instructions to create the StockWeb project. It is simplest
to download and un-jar the
appropriate
source code
after copying them to C:\temp:
cd /d C:\temp
jar xvf StockWeb.war
You will need to create a services package under Java Resources: src,
and when his instructions tell you to import files (New/Other/General/File System),
select them from the
C:\temp\WEB-INF\classes\services directory into your services folder.
When creating a web service, it is NOT available from the .class file's menu,
but rather from the .java file (Java Resources: src/services).
His "Figure 2", representing the specification of the "Web Service",
has changed its appearance with our more recent
version of Eclipse, but his explanations of old options are illuminating.
They indicate how to create and test a web service and a client interacting
with the service. Better instructions for our version are found below, but
a good explanation of what is going on is in his article.
- Move the top slider up to Test Service from Start Service
to test the service.
- When he mentions "Generate a Proxy", this is the same field as the current
Client Type: Java Proxy.
- Move the second slider from the top from No client to Test client.
This will cause a web services client to be created.
- Click Next to set up the "Web Service Java Bean Identity".
If a "Windows Security Alert" window pops up, click OK.
- Click Next to set up the .wsdl file and configure the service.
- Start the server.
- The click Finish to publish and test the service.
- Define your servlet class (e.g. SnoopServlet) and JSP code (showBrowser.jsp), and
update the web.xml content.
- Create a new local Server (New.../Other.../Server/Server)
Add your project to the server.
- Start the local server, which publishes or deploys it. It should show "Started"
as its status.
- Test the application.
For example, let's say you decided to name your project "DynamicWebProject",
and an alias for your servlet (in web.xml) is "snoop". You could reference
this in a local browser as follows:
http://localhost:8080/DynamicWebProject/snoop
which should display information about the "user-agent" if successful.
This section is under construction and has not been well tested.
- Set a breakpoint in the servlet (e.g., on if( userAgent != null )).
- Start the Server in Debug mode -- it should restart, and show "Debugging" as
its status.
- Run the application by entering the URL in the browser -- the browser
should hang.
- Look at your Eclipse session -- it is probably prompting you to
"Confirm Perspective Switch", as it changes from a "Java" perspective
to a "Debugging" perspective.
- Look at variable values, step through code, and/or resume execution, all via
the Debug menu item choices.
- Eventually, the browser will either return with the expected results, or if you
stopped the debugging session, it may continue to hang until you click on the "Stop"
button.
Concisely:
- To deploy using repos:
- Start up the remote Tomcat service on repos with the debug option
- To debug using repos:
- Shutdown any existing remote Tomcat service on repos
- Start up the remote Tomcat service on repos with the debug option
- On your computer using Eclipse:
- Make sure your application is deployed
- Setup a Remote Java Application using repos.insttech.washington.edu and your
debug port number, apply it and click on Debug
- Set a breakpoint in your code
- Use a browser or web explorer with your URL to invoke your servlet.
- Handle the debugging perspective switch and debug your code.
For more detailed instructions,
follow this link to find out how to start your own copy of a remote tomcat
service on repos.insttech.washington.edu, as well as how to deploy
to it and debug an application running on that remote service from your
workstation.
Change Log
23 Jan 2007 |
Minor editorial correction to final version of web.xml file. |
18 Jan 2007 |
Made corrections and added confirmation of correct work based on the first
ITW201 workshop. |
16 Jan 2007 |
Revised simple web application, to make it simpler to enter and understand;
Moved MySQL section and Install sections to their own pages |
27 Dec 2006 |
Added simple web service example, in part |
22 Dec 2006 |
Added information about remote tomcat shutdown and "startup debug" to debug;
Clarified and expanded section on mysql administration and database user creation |
14 Dec 2006 |
Original document |
Hours
|
Support Information
|
News
|
Policies
|
Emergencies
|