Support Environments for Java Servlets
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.
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.
is inserted in the Service tag.
Here is a sample of server.xml.
. . .
. . .
. . .
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”
Welcome to Tomcat
Welcome to Tomcat
myServlet
myPackage.myServlet
key1
value1
key2
value2
myServlet
/GetMyServlet
By this mapping you can use a custom URL to access this Servlet component otherwise you need to use myPackage.myServlet. The 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.
.
• Now we need to install Java SE Runtime Environment, JRE 6 Update 14 which can be downloaded from the following link.
.
• Now we need to install Apache Tomcat 6 from the following site :
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”
[pic]
• 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 , it should display the following screen indicating that tomcat is successfully installed.
[pic]
Here is an example of a HTTP form which makes a request to an HTTP Servlet. .
Processing post requests with data
Convert from Feet to Meters.
Enter the feet value in the field.
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.
[pic]
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 :
[pic]
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.
[pic]
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.
[pic]
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.
[pic]
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.
[pic]
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.
[pic]
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 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.
[pic]
Run Tomcat server and then execute your Servlet :Now to run the Servlet enter the following URL in the browser .
This will upload the index page to enter the value to be converted from Fahrenheit to Celsius.The page will look as follows:
[pic]
Now once clicking on submit it should load the Servlet that will display the Celsius value as follows:
[pic]
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- autism support services for adults
- support systems for mental health
- good study environments for students
- online courses for java programming
- file hash for java jre downloads
- support groups for bladder cancer
- support strategy for guided reading
- behavior support plan for pica
- family support groups for alzheimer s
- support groups for dementia
- dementia support group for caregivers
- dementia support groups for family members