Download Servlet-Tomcat

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

URL shortening wikipedia , lookup

URL redirection wikipedia , lookup

Transcript
Support Environments for Java Servlets
A Java Servlet application is supported by its Servlet container. The container may be an add-on Servlet
container or standalone container, which comes as a part of a Web server. Since a Java Servlet itself is a
Java class it also needs Java API support, specifically the Java Servlet API that is available in an archive
file called servlet-api.jar in Tomcat.
The Apache Tomcat Web server is the official reference implementation of Servlet containers, supporting
Servlets and JSP. Tomcat itself can be a standalone Web server and can also be an add-on Servlet/JSP
engine/container for other Web servers. Tomcat 6.x is the newest edition at the time of this writing, The
Tomcat Web server is an open source Servlet container originally developed by Sun Microsystems. There
are many other Web servers supporting Servlets and JSP, such as Sun’s Java Web server, and Macromedia
s JRun, Caucho Resin, and Jetty. Many application servers, like Sun Java System Application Server, BEA
WebLogic and IBM WebSphere, Oracle Application Server, Pramati Server, JBoss also support Servlets
and JSP.
3.2.1 Web Server Configuration (server.xml)
An XML format file called server.xml is used to control and configure the behavior and setting of the
Tomcat Web server. This file is located in the conf subdirectory of the Tomcat installation directory.
Some common changes needed in the configuration of Tomcat Web server may be:
1. Reset the server port number where the Servlet class or other Web component will listen for requests.
<Connector
port=”80” … />
where 8080 is the initial port number. It is replaced by 80 to make it much more convenient for clients to
access Servlets since 80 is the default HTTP port.
2. Turn on the Servlet reloading so that you don’t need to reload the recompiled Servlet.
<DefaultContext reloadable=”true”/> is inserted in the Service tag.
Here is a sample of server.xml.
. . .
<Server port="8005" shutdown="SHUTDOWN" debug="0">
. . .
<Service name="Catalina">
<Connector port="80" maxThreads="150" minSpareThreads="25"
maxSpareThreads="75" enableLookups="false" redirectPort="8443"
acceptCount="100" debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
<Engine name="Catalina" defaultHost="localhost" debug="0">
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true"
autoDeploy="true">
. . .
<DefaultContext reloadable="true" />
<Context path="" docBase="ROOT" debug="0" />
</Host>
</Engine>
</Service>
</Server>
3.2.2 Java Servlet Deployment Descriptor (web.xml)
An XML format file called web.xml in WEB-INF subdirectory of your Web application directory is used to
control and configure the behavior and setting of Java Servlets in your specific Web application. There is
another server-wide web.xml in the same place as server.xml. The server-wide configurations will apply to
all Web components on the server. As each application is deployed, this file is processed first, followed
by the "/WEB-INF/web.xml" deployment descriptor from your own applications. The application specific
resource configurations should go in the "/WEB-INF/web.xml" in your application.
Here is a sample Web server-wide web.xml for Tomcat 5. You generally don’t need to change the
serverwide web.xml configuration file most of the time.
The following is an example of an application specific web.xml for the myServlet class in the package
“myPackage”
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>Welcome to Tomcat</description>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>myPackage.myServlet</servlet-class>
<init-param>
<param-name>key1</param-name>
<param-value>value1</param-value>
</init-param>
<init-param>
<param-name>key2</param-name>
<param-value>value2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/GetMyServlet</url-pattern>
</servlet-mapping>
</web-app>
By this mapping you can use a custom URL http://host/<yourWebAppName>/GetMyServlet to
access this Servlet component otherwise you need to use http://host/<yourWebAppName>/servlet/
myPackage.myServlet. The <init-param> initializes key/value pairs which can be accessed by Servlet’s
ServletConfig.getInitParameter().
3.2.3 Other Configurations
There are some other configurations such as path, classpath, JAVA_HOME, and TOMCAT_HOME
environment settings. The environment variable JAVA_HOME is set to the Java installation directory and
the environment variable path is set to bin sub-directory by command line or by Windows Control Panel
=> System Setting after you install JSDK. The environment variable TOMCAT_HOME is set to the
Tomcat installation directory and the environment variable classpath needs to point to
install_dir/common/lib/servlet-api.jar and install_dir/common/lib/jsp-api.jar in order to make Java API of
Servlet and JSP available. Often you need to have current working directory “.” be included in the
classpath. The detail settings are showed in the Lab1 in 3.5 Lab section.
3.2.4 Tomcat 6.x Set up

First we need to install Java SE Development Kit (JDK) , JDK 6 Update 14
which can be downloaded from the following link.
http://java.sun.com/javase/downloads/index.jsp.
 Now we need to install Java SE Runtime Environment, JRE 6 Update 14
which can be downloaded from the following link.
http://java.sun.com/javase/downloads/index.jsp.
 Now we need to install Apache Tomcat 6 from the following site :
http://tomcat.apache.org/download-60.cgi
6.0.20->binary Distributions ->Core ->Windows Service installer
 Once the tomcat installation is done we need to set the JAVA_HOME
environment variables. To create these environment variables in Windows 2000
or Windows XP, go to Start -> Settings -> Control Panel -> System -> Advanced
-> Environment Variables -> System variables -> New. Enter the name and value
for JAVA_HOME as “C:\Program Files (x86)\Java\jdk1.6.0_14”


Now start the Tomcat Server by Start-> All Programs ->Apache Tomcat 6.0 >Monitor Tomcat . A small icon will appear on the bottom right of the screen
.Now Right Click this Icon and select Start Service.
Now once the service has started open the browser and type the following URL ,
http://localhost:8080/ it should display the following screen indicating that tomcat
is successfully installed.
Here is an example of a HTTP form which makes a request to an HTTP Servlet. .
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Processing post requests with data</title>
</head>
<body>
<form action = " /conv/conversion" method = "post">
<p><label>
Convert from Feet to Meters.
Enter the feet value in the field.
<br /><input type = "text" name = "feet" />
<input type = "submit" value = "Submit" />
</p></label>
</form>
</body>
</html>
You can see the POST method type is specified in this form request to a Servlet named conversion as the
action attribute of the form tag. The Servlet class name is not necessarily the same as this name since the
deployment descriptor may map a virtual name to the actual Servlet class name.
The HTTP form which invokes the Servlet conversion is shown here. “conv” is this Web application’s
ROOT directory.
3.3.2
My First Java Servlet on Tomcat
Now lets write a Servlet that will take input from the in Fahrenheit and converts into
Celsius. The code for the program is as follows :
Create your Web application folder : The next step is to create your web application
folder. The name of the folder can be any valid and logical name that represents your
application (e.g. bank_apps, airline_tickets_booking, shopping_cart,etc). But the most
important criterion is that this folder should be created under webapps folder. The path
would be similar or close to this - C:\Program Files\Apache Software Foundation\Tomcat
6.0\webapps. For demo purpose, let us create a folder called conv under the webapps
folder.
Create the WEB-INF folder : The next step is to create the WEB-INF folder. This
folder should be created under your web application folder that you created in the
previous step. The following figure WEB-INF folder being placed under the conv folder.
Also place the index.html file in this folder.
Create the web.xml file and the classes folder : The next step is to create the web.xml
file and the classes folder. Ensure that the web.xml and classes folder are created under
the WEB-INF folder. The following Figure shows this file and folder being placed under
the WEB-INF folder.
Copy the servlet class to the classes folder : We need to copy the servlet class file
to the classes folder in order to run the servlet that we created.
Edit web.xml to include servlet’s name and url pattern : This step involves two
actions viz. including the servlet’s name and then mentioning the url pattern. Let us
first see as how to include the servlet’s name in the web.xml file. Open the web.xml
file and include the servlet’s name as shown.
Note: The servlet-name need not be the same as that of the class name. You can give
a different name (or alias) to the actual servlet. This is one of the main reasons as why
this tag is used for.
Next, include the url pattern using the <servlet-mapping> </servlet-mapping> tag.
The url pattern defines as how a user can access the servlet from the browser.
Following Figure shows the url pattern entry for our current servlet.
Run Tomcat server and then execute your Servlet :Now to run the Servlet enter the
following URL in the browser .
http://localhost:8080/conv/index.html
This will upload the index page to enter the value to be converted from Fahrenheit
to Celsius.The page will look as follows:
Now once clicking on submit it should load the Servlet that will display the Celsius
value as follows: