|
Throughout this guide, it is assumed that:
- JAVA_HOME = D:\Program Files\JDK1.3.1_01
- JAVA_RUNTIME_ENVIRONMENT = C:\Program Files\JavaSoft\JRE\1.3.1\bin\hotspot\jvm.dll
- APACHE_HOME = D:\Program Files\Apache Group\Apache
- TOMCAT_HOME = D:\Program Files\Apache Tomcat 4.0
Download the Demo sample,
extract it and test it.
- Apache and its Tomcat Modules:
- Download Apache and install it under "d:\program files\apache group\apache".
- Download its Tomcat Module: "webapp-module-1.0-tc40-windows.zip".
- Extract "mod_web.so" and "libapr.dll", and place them under
"d:\program files\apache group\apache\modules".
Copy & Paste the "Start Apache" and "Stop Apache" icons to the desktop
from "C:\WINDOWS\Start Menu\Programs\Apache Web Server" (Windows Explorer)
Edit "httpd.conf" from "D:\Program Files\Apache Group\Apache\conf".
Search for "#ServerName new.host.name" and add the following after that:
ServerName localhost
At the end of the "httpd.conf" file, add the following:
LoadModule webapp_module "d:\progra~1\apache~1\Apache\modules\mod_webapp.so"
WebAppConnection warpConnection warp localhost:8008
WebAppDeploy demo warpConnection /demo/
WebAppInfo /webapp-info
To start Apache, double-click on the "Start Apache" desktop icon.
Open up a web browser and type in the URL:
http://localhost:80/index.html
Integrate the Tomcat Servlet Container with the Apache Web Server:
- Shutdown Apache by double-clicking on the "Stop Apache"
icon on the desktop. Also close the web browser.
- Download and install Tomcat 4.0 under
"D:\Program Files\Apache Tomcat 4.0".
Copy & Paste the Start Tomcat and Stop Tomcat icons to the desktop
from "C:\WINDOWS\Start Menu\Programs\Apache Tomcat 4.0" (Windows Explorer)
- Download and install Tomcat 4.0 under
"D:\Program Files\Apache Tomcat 4.0".
Modify the "server.xml" file under "d:\Program Files\Apache Tomcat 4.0\conf\"
so that Tomcat will run under Apache:
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<!--
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8080" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="10" debug="0" connectionTimeout="60000"/>
-->
- Create Tomcat's Deployment Directory:
Create directory "d:\Program Files\Apache Tomcat 4.0\webapps\demo".
- Create a Tomcat JSP file:
Create "demo.jsp" under "D:\Program Files\Apache Tomcat 4.0\webapps\demo":
<%
out.println("Hello World");
%>
Start Tomcat then start Apache only after Tomcat has started.
Open up a web browser and type in the URL:
http://localhost:80/demo/demo.jsp
- Preparing for
a (Java / Bean / JSP / Servlet / XML) Sample:
- Restart Tomcat to clear all the Servlets in memory.
- Create the directory for "JSP" under
"D:\Program Files\Apache Tomcat 4.0\webapps\demo".
- Create the directory for "JAVA" and "SERVLET" under
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes".
- Create the directory for "BEAN" under
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes\packagedemo".
- The lifecycle of a successful web transaction from the sample:
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes\packagedemo".
======= ======= ======= =======
|| || || || || || || ||
|| 1 || || 2 || || 3 || || 4 ||
|| \/ || \/ || \/ || \/
(Action)BEAN (Action)JSP (Action)SERVLET (Action)HANDLER (Action)XMLHANDLER
/\ || /\ || /\ || /\ ||
|| 8 || || 7 || || 6 || || 5 ||
|| || || || || || || ||
======= ======= ======= =======
Where (Action) are HTML form actions such as "login" and "menu".
e.g. Action: login
1. "login.jsp" calls "LoginBean" to create the Login HTML form
2. "login.jsp" submits username and password to the "LoginServlet".
3. "LoginServlet" creates an XML message to the "LoginHandler".
4. "LoginHandler" processes the XML message with an XML Handler.
5. The XML Handler will extract the necessary data for Login
i.e. username and password from the XML Message, back to the caller "LoginHandler".
6. "LoginHandler" will process the parsed data and creates an XML output.
7. "LoginServlet" will get the XML output from "LoginHandler",
sets the servlet session values from the XML output and it
will forward an appropriate JSP back to the client, e.g. web browser.
8. "menu.jsp" will be called if a valid XML output was generated
otherwise "error.jsp" will be called if no XML output was generated.
Create the Interface source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes\packagedemo".
Create the Bean source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes\packagedemo".
-
BeanHtml.java:
package packagedemo;
import java.lang.*;
public class BeanHtml extends java.lang.Object
{
public java.lang.String htmlString;
public BeanHtml
(
)
{
super();
this.setHtmlString("");
return;
}
public void addPage
(
)
{
this.setHtmlString
(
this.getHtmlString()
);
return;
}
public void addSeparator
(
)
{
this.setHtmlString
(
this.getHtmlString() + "\n<hr width=\"100\">"
);
return;
}
public void setHtmlString
(
java.lang.String htmlStringValue
)
{
this.htmlString = new java.lang.String(htmlStringValue);
return;
}
public java.lang.String getHtmlString
(
)
{
return this.htmlString;
}
public void addParameter
(
java.lang.String nameValue,
java.lang.String valueValue
)
{
this.setHtmlString
(
this.getHtmlString() +
"\n<br>" + nameValue +
"=" + valueValue
);
return;
}
}
Create the JSP source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo".
-
menu.jsp:
<%@ page import="packagedemo.*" %>
<jsp:useBean id="beanMenu_id" class="packagedemo.BeanMenu" />
<jsp:setProperty name="beanMenu_id" property="*" />
<%!
BeanMenu beanMenu;
%>
<%
java.util.Enumeration enumeration;
java.lang.String name;
java.lang.String value;
if (
(request.getRequestURL().toString().equals
(
"http://localhost" +
packagedemo.Common.URL_LOGINSERVLET
) != true) ||
(request.getMethod().equals("POST") != true)
)
{
%>
<jsp:forward page="/error.jsp" />
<%
}
beanMenu = new BeanMenu();
beanMenu.addMenu();
beanMenu.addSeparator();
enumeration = request.getSession().getAttributeNames();
while (enumeration.hasMoreElements() == true)
{
name = (java.lang.String) enumeration.nextElement();
value = (java.lang.String) request.getSession().getAttribute(name);
beanMenu.addParameter(name, value);
}
out.print(beanMenu.getHtmlString());
%>
Create the Servlet source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes".
-
ActionServlet.java:
import java.lang.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import ActionServletXMLHandler;
import packagedemo.*;
import ActionHandler;
public class ActionServlet extends javax.servlet.http.HttpServlet
{
private javax.servlet.http.HttpServletRequest request;
private javax.servlet.http.HttpServletResponse response;
private javax.servlet.http.HttpSession session;
private javax.xml.parsers.SAXParserFactory saxParserFactory;
private javax.xml.parsers.SAXParser saxParser;
private ActionServletXMLHandler actionServletXMLHandler;
private java.io.StringReader stringReader;
private org.xml.sax.InputSource inputSource;
private ActionHandler actionHandler;
private java.lang.String name;
private java.lang.String value;
private java.lang.String formXML;
public ActionServlet
(
)
{
super();
// SAX Parser Factory
this.saxParserFactory =
javax.xml.parsers.SAXParserFactory.newInstance();
// SAX Parser
try
{
this.saxParser = this.saxParserFactory.newSAXParser();
}
catch
(
java.lang.Exception exceptionValue
)
{
java.lang.System.err.println(exceptionValue.toString());
return;
}
return;
}
// For GET Form Submissions
public void doGet
(
javax.servlet.http.HttpServletRequest requestValue,
javax.servlet.http.HttpServletResponse responseValue
)
{
// GET method not allowed, return error page.
this.forwardJSP(packagedemo.Common.URL_ERRORJSP);
return;
}
// For POST Form Submissions
public void doPost
(
javax.servlet.http.HttpServletRequest requestValue,
javax.servlet.http.HttpServletResponse responseValue
)
{
java.lang.String action;
// Refresh: Request, Response, Session
this.setRequest(requestValue);
this.setResponse(responseValue);
this.get();
if (this.check() != true)
{
this.forwardJSP(packagedemo.Common.URL_ERRORJSP);
return;
}
this.set();
return;
}
public void get
(
)
{
// Set session
this.setSession(this.getRequest().getSession(true));
// Prepare Form XML
this.getActionHandler().setXMLOutput(packagedemo.Common.STRING_EMPTY);
this.createFormXML();
// Process response from Action Handler
this.getActionHandler().start(this.getFormXML());
return;
}
public boolean check
(
)
{
boolean flag;
flag = true;
// No valid response, throw error
if (this.getActionHandler().getXMLOutput().
equals(packagedemo.Common.STRING_EMPTY) == true)
{
flag = false;
return flag;
}
return flag;
}
public void set
(
)
{
this.updateSession();
return;
}
public void updateSession
(
)
{
// XML Handler
this.actionServletXMLHandler =
new ActionServletXMLHandler(this);
// Parse
try
{
this.stringReader =
new java.io.StringReader(this.getActionHandler().getXMLOutput());
this.inputSource =
new org.xml.sax.InputSource
(
(java.io.Reader)
this.stringReader
);
this.saxParser.parse
(
this.inputSource,
(org.xml.sax.helpers.DefaultHandler)
this.actionServletXMLHandler
);
}
catch
(
java.lang.Exception exceptionValue
)
{
java.lang.System.err.println(exceptionValue.toString());
return;
}
return;
}
public void kickTrigger
(
)
{
this.getSession().setAttribute
(
this.getName(), this.getValue()
);
return;
}
public void forwardJSP
(
java.lang.String jspValue
)
{
try
{
super.getServletConfig().getServletContext().
getRequestDispatcher(jspValue).
forward(this.getRequest(), this.getResponse());
}
catch
(
java.lang.Exception exceptionValue
)
{
return;
}
return;
}
public void createFormXML
(
)
{
java.util.Enumeration enumeration;
java.lang.String name;
java.lang.String value;
this.setFormXML("<form>");
enumeration = this.getRequest().getParameterNames();
while (enumeration.hasMoreElements() == true)
{
name = (java.lang.String) enumeration.nextElement();
value = (java.lang.String) this.getRequest().getParameter(name);
this.setFormXML
(
this.getFormXML() +
"<" + name + ">" + value +
"</" + name + ">"
);
}
this.setFormXML(this.getFormXML() + "</form>");
return;
}
public void setRequest
(
javax.servlet.http.HttpServletRequest requestValue
)
{
this.request = requestValue;
return;
}
public javax.servlet.http.HttpServletRequest getRequest
(
)
{
return this.request;
}
public void setResponse
(
javax.servlet.http.HttpServletResponse responseValue
)
{
this.response = responseValue;
return;
}
public javax.servlet.http.HttpServletResponse getResponse
(
)
{
return this.response;
}
public void setSession
(
javax.servlet.http.HttpSession sessionValue
)
{
this.session = sessionValue;
return;
}
public javax.servlet.http.HttpSession getSession
(
)
{
return this.session;
}
public void setFormXML
(
java.lang.String formXMLValue
)
{
this.formXML = new java.lang.String(formXMLValue);
return;
}
public java.lang.String getFormXML
(
)
{
return this.formXML;
}
public void setName
(
java.lang.String nameValue
)
{
this.name = new java.lang.String(nameValue);
return;
}
public java.lang.String getName
(
)
{
return this.name;
}
public void setValue
(
java.lang.String valueValue
)
{
this.value = new java.lang.String(valueValue);
return;
}
public java.lang.String getValue
(
)
{
return this.value;
}
public void setActionHandler
(
ActionHandler actionHandlerValue
)
{
this.actionHandler = actionHandlerValue;
return;
}
public ActionHandler getActionHandler
(
)
{
return this.actionHandler;
}
}
Create the Handler source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes".
-
ActionHandler.java:
import java.lang.*;
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import packagedemo.*;
import ActionHandlerXMLHandler;
public class ActionHandler extends java.lang.Object
{
private javax.xml.parsers.SAXParserFactory saxParserFactory;
private javax.xml.parsers.SAXParser saxParser;
private ActionHandlerXMLHandler actionHandlerXMLHandler;
private java.io.StringReader stringReader;
private org.xml.sax.InputSource inputSource;
private java.lang.String xmlInput;
private java.lang.String xmlOutput;
private java.lang.String name;
private java.lang.String value;
public ActionHandler
(
)
{
super();
// SAX Parser Factory
this.saxParserFactory =
javax.xml.parsers.SAXParserFactory.newInstance();
// SAX Parser
try
{
this.saxParser = this.saxParserFactory.newSAXParser();
}
catch
(
java.lang.Exception exceptionValue
)
{
java.lang.System.err.println(exceptionValue.toString());
return;
}
// XML Strings
this.setXMLInput(packagedemo.Common.STRING_EMPTY);
this.setXMLOutput(packagedemo.Common.STRING_EMPTY);
return;
}
public void start
(
java.lang.String xmlValue
)
{
this.setXMLInput(xmlValue);
this.get();
if (this.check() != true)
{
return;
}
this.set();
}
public void get
(
)
{
// XML Handler
this.actionHandlerXMLHandler =
new ActionHandlerXMLHandler(this);
// Parse
try
{
this.stringReader =
new java.io.StringReader(this.getXMLInput());
this.inputSource =
new org.xml.sax.InputSource
(
(java.io.Reader)
this.stringReader
);
this.saxParser.parse
(
this.inputSource,
(org.xml.sax.helpers.DefaultHandler)
this.actionHandlerXMLHandler
);
}
catch
(
java.lang.Exception exceptionValue
)
{
java.lang.System.err.println(exceptionValue.toString());
return;
}
return;
}
public boolean check
(
)
{
boolean flag;
flag = true;
return flag;
}
public void set
(
)
{
this.createXMLOutput();
return;
}
public void createXMLOutput
(
)
{
return;
}
public void kickTrigger
(
)
{
return;
}
public void setName
(
java.lang.String nameValue
)
{
this.name = new java.lang.String(nameValue);
return;
}
public java.lang.String getName
(
)
{
return this.name;
}
public void setValue
(
java.lang.String valueValue
)
{
this.value = new java.lang.String(valueValue);
return;
}
public java.lang.String getValue
(
)
{
return this.value;
}
public void setXMLInput
(
java.lang.String xmlValue
)
{
this.xmlInput = new java.lang.String(xmlValue);
return;
}
public java.lang.String getXMLInput
(
)
{
return this.xmlInput;
}
public void setXMLOutput
(
java.lang.String xmlValue
)
{
this.xmlOutput = new java.lang.String(xmlValue);
return;
}
public java.lang.String getXMLOutput
(
)
{
return this.xmlOutput;
}
}
Create the XML Handler source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes".
-
XMLHandler.java:
import java.lang.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import packagedemo.*;
public class XMLHandler extends org.xml.sax.helpers.DefaultHandler
{
private java.lang.Object object;
public XMLHandler
(
java.lang.Object objectValue
)
{
super();
this.setObject(objectValue);
return;
}
public void startDocument
(
)
throws org.xml.sax.SAXException
{
super.startDocument();
return;
}
public void endDocument
(
)
throws org.xml.sax.SAXException
{
super.endDocument();
return;
}
public void startElement
(
java.lang.String namespaceUriValue,
java.lang.String localNameValue,
java.lang.String qualifiedNameValue,
org.xml.sax.Attributes attributesValue
)
throws org.xml.sax.SAXException
{
super.startElement
(
namespaceUriValue,
localNameValue,
qualifiedNameValue,
attributesValue
);
return;
}
public void endElement
(
java.lang.String namespaceUriValue,
java.lang.String simpleNameValue,
java.lang.String qualifiedNameValue
)
throws org.xml.sax.SAXException
{
super.endElement
(
namespaceUriValue,
simpleNameValue,
qualifiedNameValue
);
return;
}
public void characters
(
char bufferValue[],
int offsetValue,
int lengthValue
)
throws org.xml.sax.SAXException
{
super.characters(bufferValue, offsetValue, lengthValue);
return;
}
public void processingInstruction
(
java.lang.String targetValue,
java.lang.String dataValue
)
throws org.xml.sax.SAXException
{
super.processingInstruction(targetValue, dataValue);
return;
}
public void setObject
(
java.lang.Object objectValue
)
{
this.object = objectValue;
return;
}
public java.lang.Object getObject
(
)
{
return this.object;
}
}
-
ActionHandlerXMLHandler.java:
import java.lang.*;
import packagedemo.*;
import XMLHandler;
import ActionHandler;
public class ActionHandlerXMLHandler extends XMLHandler
{
public ActionHandlerXMLHandler
(
ActionHandler actionHandlerValue
)
{
super((java.lang.Object) actionHandlerValue);
return;
}
public void startElement
(
java.lang.String namespaceUriValue,
java.lang.String localNameValue,
java.lang.String qualifiedNameValue,
org.xml.sax.Attributes attributesValue
)
throws org.xml.sax.SAXException
{
((ActionHandler) super.getObject()).setName(qualifiedNameValue);
((ActionHandler) super.getObject()).setValue(packagedemo.Common.STRING_EMPTY);
return;
}
public void endElement
(
java.lang.String namespaceUriValue,
java.lang.String simpleNameValue,
java.lang.String qualifiedNameValue
)
throws org.xml.sax.SAXException
{
((ActionHandler) super.getObject()).kickTrigger();
return;
}
public void characters
(
char bufferValue[],
int offsetValue,
int lengthValue
)
throws org.xml.sax.SAXException
{
((ActionHandler) super.getObject()).setValue
(
new java.lang.String
(
new java.lang.String(bufferValue)
).substring(offsetValue, offsetValue + lengthValue)
);
return;
}
}
-
ActionServletXMLHandler.java:
import java.lang.*;
import packagedemo.*;
import XMLHandler;
import ActionServlet;
public class ActionServletXMLHandler extends XMLHandler
{
public ActionServletXMLHandler
(
ActionServlet actionServletValue
)
{
super((java.lang.Object) actionServletValue);
return;
}
public void startElement
(
java.lang.String namespaceUriValue,
java.lang.String localNameValue,
java.lang.String qualifiedNameValue,
org.xml.sax.Attributes attributesValue
)
throws org.xml.sax.SAXException
{
((ActionServlet) super.getObject()).setName(qualifiedNameValue);
((ActionServlet) super.getObject()).setValue(packagedemo.Common.STRING_EMPTY);
return;
}
public void endElement
(
java.lang.String namespaceUriValue,
java.lang.String simpleNameValue,
java.lang.String qualifiedNameValue
)
throws org.xml.sax.SAXException
{
((ActionServlet) super.getObject()).kickTrigger();
return;
}
public void characters
(
char bufferValue[],
int offsetValue,
int lengthValue
)
throws org.xml.sax.SAXException
{
((ActionServlet) super.getObject()).setValue
(
new java.lang.String
(
new java.lang.String(bufferValue)
).substring(offsetValue, offsetValue + lengthValue)
);
return;
}
}
Compile the packagedemo source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes\packagedemo".
del *.class
d:\progra~1\jdk1.3.1_01\bin\javac.exe -classpath ".;d:\Progra~1\j2sdkee1.3\lib\j2ee.jar" *.java
Compile the classes source files under the directory
"D:\Program Files\Apache Tomcat 4.0\webapps\demo\WEB-INF\classes".
del *.class
d:\progra~1\jdk1.3.1_01\bin\javac.exe -classpath ".;d:\Progra~1\j2sdkee1.3\lib\j2ee.jar" *.java
Java Servlets
-
After compiling the changes made to servlet source codes, it
might be necessary to restart (stop and start) Tomcat again
to run the new servlets.
|