Web applications are client/server applications. The web browser is the client. The server is a program running on some computer which is identified by a number called the port to distinguish it from other server programs. Clients access the server program via the pair (server,port) called a socket. The browser client (as well as other client programs) initiates the interaction with a URL request:
protocol://data_source
where the protocol is any number of possiblities, including http, file, https, ftp, etc. The data_source can be any number of things from a regular file on the client's machine to a file or program on some server such as these:
file://file
http://server/dir1/dir2/file
http://server/dir1/program
http://server:port/file (server running on alternate port)
The protocol normally dictates the port, but we can specify it if it's different than the usual one. If the target file is a directory, then a default file or program is chosen as the source.
Server-side web programming means that an HTTP request from a browser causes the web server to run a program which generates HTML (or other data) and sends it back to the browser. Java uses Servlets and Java Server Pages (JSP) for server-side programming. These two software technologies are Java's contribution to the two primary styles of server-side programming:
HTML code generated entirely by programming: Perl/CGI, Servlets
program code embedded within HTML code: PHP, ASP, JSP
Server-side programming stands in contrast to client-side programming which is usually equated with JavaScript, but can also be effected by Java Applets, Flash, or any others which provide a browser plugin.
Tomcat web server
A Java Servlet is an object that can the executed by an HTTP server due to the invocation of a browser client. This ability is not automatically part of standard web servers like Apache and Microsoft's IIS so we need a specialized web server for that purpose. The Apache open source tomcat web server serves our needs; its website is this:
http://tomcat.apache.org/
In order to permit user access and not to conflict with other web services — the standard web service runs on port 80, a restricted port in UNIX system — tomcat runs on the standard port 8080. The Tomcat web server starts a Java virtual machine (JVM) and handles browser client accesses by executing the servlets in threads created by this JVM which makes the entire web service process very fast, in contrast, say to CGI programming in which each web server activation requires a new process creation.
Eclipse Web applications setup
The Eclipse IDE supports many plugin tools and allows developers to directly create and modify plugins. It's not clear at this stage of development what will be the definitive plugin tool for web development but the so-called webtools development packages found at
http://www.eclipse.org/webtools/
seems like a likely contender. We'll use the webtools current release build, version R-1.5.2-200610261841, which requires eclipse version 3.1.1. The current stable webtools release is 1.5M4, but this requires eclipse version 3.2M4.
The eclipse webtools and support plugins you'll need are these:
wtp-sdk-R-1.5.2-200610261841.zip
emf-sdo-xsd-SDK-2.2.1.zip
GEF-SDK-3.2.1.zip
JEM-SDK-1.2.1.zip
The Apache tomcat web server, currently version 6.0.14, uses this distribution file, also available through the tomcat website:
apache-tomcat-6.0.14.zip
Install Webtools
Assuming that eclipse 3.1.1 is installed as the directory "eclipse", extract these zip archives:
wtp-sdk-R-1.5.2-200610261841.zip
emf-sdo-xsd-SDK-2.2.1.zip
GEF-SDK-3.2.1.zip
JEM-SDK-1.2.1.zip
into the directory containing the eclipse subdirectory (e.g., the C:\ directory in my suggested Windows installation of Eclipse). These zip archives already have eclipse as their top-level directory, and so their contents will be extracted correctly into the eclipse subdirectory.
The zip files extract almost entirely into the features and plugins subdirectories of eclipse. The EMF zip archive contains duplicates of the top-level files epl-v10.html and notice.html You can either let them be overwritten or not — it is unimportant.
Install Tomcat
Unzip the file apache-tomcat-6.0.14.zip into your folder of choice; it creates the subdirectory
apache-tomcat-6.0.14
A good choice for location might be the same directory holding eclipse.
Tomcat Server Setup in Eclipse
Open Eclipse and select New Other. You should see quite a number of new categories now available due to the plugin additions.
To create the Tomcat server:
Open the Server category, select the Server entry within and click Next.
In the Define a New Server window, select Apache Tomcat v5.5 Server, click Next.
In the Tomcat Server window, click the Browse button to specify the Tomcat installation directory, apache-tomcat-6.0.14. Click Finish.
Specify the web browser to use
Eclipse has its own internal web browser that can be used to display web content, but you may want to use an external web browser. In eclipse, select:
Window Preferences General Web Browser
It's not clear that the so-called Default System Web Browser will find the right thing. You can add your own external browser if necessary. On a Windows installation, Internet Explorer will be among the list of browsers; you can simply select it, it takes no parameters.
For other browsers, e.g., firefox or mozilla browsers, if already on the list, select it, click Edit, and set the parameters to
-url %URL%
If not in the list, click New, fill in the Name (e.g., "Firefox"); browse to find the location of the executable and finally set the parameters as above.
The structure of a Servlet
The activation of a servlet is initiated by a client-side (browser) request. There are two types of requests: GET and POST. GET requests are most standard and always activated by typing any URL into a browser. GET requests are also those in which you see parameters delivered in the URL itself after the "?" character, a portion called the query string. POST requests are less frequent and are normally initiated by HTML forms.
A Java Servlet is an extension of the javax.servlet.http.HttpServlet. A GET or POST request activates one of the two built-in functions: doGet or doPost, respectively. Many server-side programs do not need to differentiate between these two activations and it is often common to merge the behaviors of these activations.
Servlet template
import java.io.*; // for PrintWriter, IOException
import javax.servlet.*; // for Servlet*
import javax.servlet.http.*; // for HttpServlet*
public class MyServlet extends HttpServlet implements Servlet
{
protected void
doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* ... */
}
protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* ... */
}
}
Typical content for the doGet or doPost function is as follows:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Thursday, September 6, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment