| ||||||||||||||
| ||||||||||||||
| ||||||||||||||
Tomcat NotesIndex
Setting up development environment
A note on configuring Tomcat: The script you ran to install Tomcat also carried out some configuration tasks. In particular, the <Resource> element found towards the end of the file ~/apache-tomcat/conf/context.xml configures a JNDI data source that can be used by your web applications. With such a data source, your web applications do not need to worry about any database- or connection-specific details; they simply request a connection from the data source. The application code for dealing with data sources can be found in ~/web-db-beers/src/my/db/BeerDB.java. Additional notes on Tomcat configuration can be found in ~/apache-tomcat/README-cps116.txt. Running TomcatTo start Tomcat, run the command "~/apache-tomcat/bin/startup.sh". To see access the Tomcat web server, point the browser on your work computer to http://localhost:8080/. Assuming that you have followed the advanced SSH setup and Tomcat installation instructions correctly, you should see a welcome page from Tomcat. To shutdown Tomcat, run the command "~/apache-tomcat/bin/shutdown.sh". Note: You can only access the Tomcat web site while you remain logged into cps116 using SSH. Also, please remember to shutdown Tomcat before you log off. Developing and deploying WARsAfter starting Tomcat, go into the directory ~/web-db-beers, which is an example of a development directory for a simple db-beers web application. Read the file build.properties in this directory to get a sense of the structure of the development directory. When you start a development directory for your own application, you will want to structure it in the same way. We will use the build tool named ant to build, deploy, and undeploy our application. Below is a list of the most essential commands (all of them should be issued from the base of the development directory, in our case ~/web-db-beers):
A typical development cycle consists of coding/debugging -> ant deploy -> testing on browser -> ant undeploy, and then back to coding/debugging. To develop your own servlet/JSP application, you may start with a copy of the development directory for db-beers, and make appropriate changes to build.properties, src/, and web/. If you want to use external jar library files, put those under web/WEB-INF/lib/. A mini-tutorial of servlet/JSPServlet basicsThe servlet concept is very simple: Instead of serving a static HTML page, the web server executes a piece of Java code to generate the HTML output to serve to the client. An example servlet is given in ~/web-db-beers/src/my/ViewDrinkerServlet.java. Note that this servlet obtains the database connection object from the current HTTP "session" object. Hence, a single database connection will be shared by all server-generated pages accessed during the same session, which is much more efficient than the approach where each page makes its own connection to access the database. The shared database connection object is created when the session is created. This task is performed by the event listener in ~/web-db-beers/src/my/listener/SessionListener.java, which listens to the event of a session being created, makes a database connection, and puts it into the session object for use by pages to be accessed during this session. Most of the database operations are handled by the BeerDB class in ~/web-db-beers/src/my/db/BeerDB.java, which wraps the database connection object. Dealing with the database server directly is a tricky business: Since many JDBC resources (such as ResultSet and Statement) require explicit release for performance reasons, exception handling can be quite tricky. Therefore, we use BeerDB to hide this complexity from the rest of the application. BeerDB.java exercises a wide range of JDBC functionalities, and the code is fairly well documented; make sure you read and understand the comments. Both the servlet and the listener need to be registered in the Web Deployment Descriptor (~/web-db-beers/web/WEB-INF/web.xml). In this file, we first use a <servlet> element to define the servlet, and then use a <servlet-mapping> element to specify the mapping between a request URL and the servlet. When a mapped URL is requested, the server will generate the response HTML by calling the associated servlet. The web.xml file will be automatically packaged inside the WAR file for deployment. When you develop your application, you will need to modify web.xml to include your own set of servlets.
JSP basicsJSP can be regarded as a convenient way of writing servlets. In fact, Tomcat implements JSP pages by translating them into servlets. The idea is to embed Java code in HTML using special tags "<% ... %>", which are executed at runtime by the server to produce the final HTML response. (The difference between JSP and JavaScript is that JavaScript is run on the client by the browser.) JSP files end with suffix .jsp. You will find several JSP examples under ~/web-db-beers/web/. Start with all-drinkers.jsp and go through edit-drinker.jsp and update-drinker.jsp. Comments in these files explain the basics of writing JSPs. There is no need to register the JSP files in the web deployment descriptor. You just need to put JSP files in the web/ subdirectory, together with regular HTML pages, GIF files, etc. They will be automatically packaged into the WAR file for deployment. Additional information
|
||||||||||||||
Last updated Mon Aug 25 14:18:22 EDT 2008 |