Pace CS 396S - The Web Server and Tomcat

Unformatted text preview:

Differences between the Web Server and TomcatRunning the ServerChanges to the Web PageThe Java ServletEcho Email ExampleThe Web Application Deployment DescriptorDeploymentDifferences between the Web Server and TomcatThe simple web server that was used at the beginning of the course has many similarities with the full Tomcat server, available at http://archive.apache.org/dist/jakarta/tomcat-5/v5.5.7/bin/. However, there areimportant differences that may cause you some problems. This document will discuss the principal ones.Running the ServerThe web server can be executed right in the IDE (Integrated Development Environment) where you createyour web pages and processing programs. The Tomcat server is opened using the file startup.bat that is found in the bin sub-folder. It should always be stopped by executing the file shutdown.bat. As before, a web page is used to communicate with the server. But now it should be in a sub-folder of the webapps folder in Tomcat, either the ROOT folder or one specific to the application.If the server was set up correctly, you can reach its home page with the URL http://localhost:8080/ or http://localhost/, depending on whether or not you changed the port from 8080 to 80. You cannot access the server by loading a local file into your browser. This works with the web server, but not with Tomcat. If you placed your web page in the ROOT folder, it can be accessed by typing http://localhost:8080/web-page-name.html/ into your browser window.Changes to the Web PageThere are a few changes required for your HTML file. The main one is that the action attribute includes the term, servlet.action="http://localhost:8080/servlet/echo.EmailServlet/"Note: This assumes that Tomcat has not been modified to receive input on port 80. If you changed the port to 80 the port does not have to be mentioned.action="http://localhost/servlet/echo.EmailServlet/"The rest of the form is the same as before.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>E-Mail Form</title></head><body><h3>Enter your name and e-mail address.<br />Then click the Send button to send the data to the server.</h3><form method = "get" action="http://localhost:8080/servlet/echo.EmailServlet"><p><input type = "text" name = "name" value = "" size = 30 /> Name </p><p><input type = "text" name = "email" value = "" size = 30 /> E-Mail Address </p><p><input type= "submit" value="Send" /></p></form>1</body> </html>The Java ServletOnly a few changes are required in order to modify the programs written for the web server. You have to import both javax.servlet and javax.servlet.http.import javax.servlet.*;import javax.servlet.http.*;These are in the package servlet.jar, so make sure that your IDE knows where to find it.The processor class now must extend HttpServlet instead of WebRequestProcessor.public class EmailServlet extends HttpServletThe main method in the servlet is now either doGet or doPost and the parameters are HttpServletRequest and HttpServletResponse.public void doGet (HttpServletRequest request, HttpServletResponse response)Finally this method always throws an IOException that must be caught or re-thrown.Echo Email Examplepackage echo;/* EmailServlet processes a request from a web page. It responds to the request by echoing back the name and email address that was sent in. */import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class EmailServlet extends HttpServlet{protected void doGet (HttpServletRequest request, HttpServletResponse response) {try{// Set the content type for the output and then get a PrintWriter object.response.setContentType ("text/html");PrintWriter out = response.getWriter ();// Get the form data from the request.String name = request.getParameter ("name"); String email = request.getParameter ("email");// Write the output header, the output data, and the footer.Page.createHeader (out, "Test Data");out.println ("<h3>Hello.</h3>");out.println ("<h3>" + name+ "</h3>");2out.println ("<h3>Your email address is " + email + "</h3>");Page.createFooter (out);}catch (IOException e) {System.out.println ("Servlet Exception");}} // doGet} // EmailServletThis method uses the same Page class that was used with the web server. It should be in the echo packagealong with the servlet.package echo;// Class with static methods that add standard lines to the html output page.protected class Page{public static void createHeader (PrintWriter out, String title){out.println ("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'>");out.println ("<html>");out.println ("<head>");out.println ("<title>" + title + "</title>");out.println ("</head>");out.println ("<body>");} // createHeaderpublic static void createFooter (PrintWriter out){out.println ("</body></html>");}} // class PageThe Web Application Deployment DescriptorThe Web Application Deployment Descriptor, web.xml, is an XML document that tells the server where to find the servlets mentioned in the action attributes in HTML forms. Various versions of web.xml comewith Apache Tomcat. They are already stored in the directory, ROOT/WEB-INF. However, the simplest one that works is the following:<?xml version="1.0" encoding="ISO-8859-1"?><web-app><servlet> <servlet-name>EmailServlet</servlet-name> <servlet-class>echo.EmailServlet</servlet-class></servlet><servlet-mapping> <servlet-name>EmailServlet</servlet-name> <url-pattern>/servlet/echo.EmailServlet</url-pattern></servlet-mapping></web-app>The <servlet> tag gives the name of the servlet and its class file. The <servlet-mapping> tag provides a short pattern that can be used to find the class file. For example, instead of 3< url-pattern>/servlet/echo.EmailServlet</url-pattern>we could have < url-pattern>/servlet/email</url-pattern>.We would then have to change the action attribute in the form toaction="http://localhost:8080/servlet/email"This example uses a package called echo. But you can name your packages any way that makes sense forthe application, including client_server.DeploymentUnlike the web server, where all files can be stored in the same folder, the placement of files for Tomcat is critical. The HTML web pages should be placed in the ROOT folder. The XML file, web.xml, belongsin the WEB-INF folder, and the Java servlets all go into the classes folder. Since the classes folder does not exist in Tomcat when installed, you should create it as a sub-folder of the WEB-INF folder. When theservlets are


View Full Document
Download The Web Server and Tomcat
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The Web Server and Tomcat and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The Web Server and Tomcat 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?