|
Developing Servlets With TJI
TJI makes the development of servlets easy by providing an integrated Web Server that requires no setup and that updates automatically when a servlet is recompiled; there is no need to reload a servlet by hand or by button, and still less need to restart the server!
Introduction The Java IDE jar (ide.jar) includes the javax.servlet and javax.servlet.http packages - so users without an 'Enterprise Edition' SDK can develop and run servlets without needing to download any extras. Servlets run on a server. Normally (after deployment), this will be a server on the web or a network. However, during development the servlets will run on the integrated Kinabaloo Web Server and you will see their results (response) either in a browser (such as Netscape or Internet Explorer) or in the integrated Web Browser (the 'Web' tab of TJI). So, you will compile your servlets in the IDE, then either call them from a separate browser after having started the Kinabaloo Web Server, or just go to the 'Web' tab and use the integrated Web Browser. When you go to the 'Web' tab for the integrated Web Browser, the Web Server will be started automatically if it is not already running. If you set the project's 'Main' or main 'HTML' file to the desired starting page (HTML file or servlet), then when you click on the 'run' button, this page will load automatically in the 'Web' browser tab. Using the integrated Web Browser has the advantage that run-time exceptions in any part of the servlet code will be caught and the exact line of source code will be shown, if the project is open, or just the exception description, file name and line number otherwise.
The Web Server Configuration You will only need to start the Web Server once - it automatically updates itself when a servlet is recompiled; that is, servlets are automatically reloaded. There is no need to explicitly reload the servlet to the web server as when using a separate web server program. From the 'WebServer' menu of the IDE, you can start and stop the web server. The web server starts automatically when you select the 'Web' tab, or if you choose 'Run' for a servlet project. Once started, the web server will handle all calls to the 'local server'. This has the IP address 127.0.0.1 and the alias 'localhost'. It is set up to retrieve files from your local file system. With The Kinabaloo Web Server, the logical root is defined as <homepath>/web/<project_name> where homepath is the location of the ide jar file (possibly ~/sdkx.x.x/bin/). If you place say an image file (for example, myImage.gif) in this directory (~/web/<project_name>) and type:
the browser will display this image; the same is true for other file types such as HTML pages (naturally). Thus, store all the resources required by the server in this 'web' directory. This includes any HTML pages that will call a servlet or are used by a servlet. The servlets are stored in a sub-directory structure of web called <project_name>/WEB-INF/classes. Hence, your servlets should be in directory <homepath>/web/<project_name>/WEB-INF/classes for the Kinabaloo Web Server to find them; this is the logical root for servlets. Therefore, to invoke MyServlet, the URL is simply :
If your servlet is in a package called, say, 'books', then the URL will be :
and the servlet code and class file will be in directory <homepath>/web/<project_name>/WEB-INF/classes/books
Port The :8080 part of the address is simply a port number. Different ports can be used, although some are used for specific purposes, such as email; in The Kinabaloo Web Server, the default is 8080 but you can change this if you wish and the new value will be used whenever the IDE is run (see the WebServer menu).
Query String It is common to pass information to a servlet by using a 'query string' that follows the servlet name and begins with a '?'. This string takes the format of key=value pairs (much like Properties) separated, if neceassry, by '&'. As an example, http://localhost:8080/MyServlet?action=checkPrice&item=62835 The keys are defined by you the servlet author.
Aliases Aliases can be set up via the 'Web Server' menu. This is useful for allowing shorter and more user-friendly addresses to be used by the end-user. For example,
Aliases are remembered from one run of TJI to the next and are stored and applied globally rather than per project. You may edit and remove aliases as well as add new ones as desired.
Automatic Session Timeout You can set the Kinabaloo Web Server to automatically invalidate sessions after a period of inactivity equal to 2, 10 or 30 minutes; the default value is 2 minutes, which is probably best for the development environment. To invalidate a session means to remove an HttpSession object and its values from the server. The Kinabaloo Web Server is multi-threaded and can handle multiple simultaneous transactions.
The Example Servlets There are many example servlet projects available from the 'Resources' page of our web site. These include:
To create and run a skeleton servlet
|