HOW TO: Setup Java Servlets in my JSP/Servlet enabled web hosting account ?

To help you get started, we are providing an example web.xml and servlet class you can use to try out a servlet. Place this web.xml in the following directory:

/home/username/public_html/WEB-INF

(replace 'username' with your account's username.)

Place the servlet Java code in the following directory and compile it to the servlet class:

/home/username/public_html/WEB-INF/classes

(You will need to create these directories if they do not exist already.)

Once you've saved the web.xml and created the servlet class, it will take just a moment for Tomcat to begin serving the servlet. Access the URL:

http://www.yourdomain.com/servlet/hello

or

http://www.yourdomain.com:8080/servlet/hello

And you should see the servlet's output. "Hello World!"

(You will need to replace 'yourdomain.com' with your web site's domain name. and replace the port number '8080' with your Tomcat instance's port number. Typically the Tomcat port number is 4 or 5 digits and ends in 80. Examples include: 8080, 9580, 10080)

Here is the sample web.xml file we used:

<?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>

<!-- General description of your web application -->

<display-name>My Web Application</display-name>
<description>
This is version X.X of an application to perform
a wild and wonderful task, based on servlets and
JSP pages. It was written by Dave Developer
(dave@mycompany.com), who should be contacted for
more information.
</description>


<!-- Context initialization parameters that define shared
String constants used within your application, which
can be customized by the system administrator who is
installing your application. The values actually
assigned to these parameters can be retrieved in a
servlet or JSP page by calling:

String value =
getServletContext().getInitParameter("name");

where "name" matches the <param-name> element of
one of these initialization parameters.

You can define any number of context initialization
parameters, including zero.
-->

<context-param>
<param-name>webmaster</param-name>
<param-value>webmaster@mywipl.com</param-value>
<description>
The EMAIL address of the administrator to whom questions
and comments about this application should be addressed.
</description>
</context-param>


<!-- Servlet definitions for the servlets that make up
your web application, including initialization
parameters. With Tomcat, you can also send requests
to servlets not listed here with a request like this:

http://localhost:8080/{context-path}/servlet/{classname}

but this usage is not guaranteed to be portable. It also
makes relative references to images and other resources
required by your servlet more complicated, so defining
all of your servlets (and defining a mapping to them with
a servlet-mapping element) is recommended.

Servlet initialization parameters can be retrieved in a
servlet or JSP page by calling:

String value =
getServletConfig().getInitParameter("name");

where "name" matches the <param-name> element of
one of these initialization parameters.

You can define any number of servlets, including zero.
-->


<servlet>
<servlet-name>hello</servlet-name>
<description>
Hello World!
</description>
</servlet>


<!-- Define mappings that are used by the servlet container to
translate a particular request URI (context-relative) to a
particular servlet. The examples below correspond to the
servlet descriptions above. Thus, a request URI like:

http://localhost:8080/{contextpath}/graph

will be mapped to the "graph" servlet, while a request like:

http://localhost:8080/{contextpath}/saveCustomer.do

will be mapped to the "controller" servlet.

You may define any number of servlet mappings, including zero.
It is also legal to define more than one mapping for the same
servlet, if you wish to.
-->

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<!-- Define the default session timeout for your application,
in minutes. From a servlet or JSP page, you can modify
the timeout for a particular session dynamically by using
HttpSession.getMaxInactiveInterval(). -->

<session-config>
<session-timeout>30</session-timeout> <!-- 30 minutes -->
</session-config>

 

</web-app>

Here is our sample Hello.java source file:

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;

public class Hello extends HttpServlet
{
public void service(HttpServletRequest _request,
HttpServletResponse _response) throws ServletException, IOException
{
_response.setContentType("text/html");

ServletOutputStream out = _response.getOutputStream();

out.println("<br><html>");
out.println("<br><head><br><title>Hello World!<br></title><br></head>");
out.println("<br><body>");
out.println("<br><h1>Hello World!<br></h1>");
out.println("<br></body>");
out.println("<br></html>");
out.close();
}

public String getServletInfo()
{
return "HelloServlet";
}
}

 

The following URL will give detailed directions on how you need to setup a web.xml file to get your servlets to work with Tomcat:

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/appdev/deployment.html

 

 

  • Email, SSL
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

What is the difference between shared SSL and private SSL?

A shared SSL displays as https://ip/~yourusername/. A private SSL would display as...

Why are my FrontPage extensions not working?

You must first make sure you have installed the extensions from going into your cpanel and...

Application Paths

Here are the applications paths. If you do not see what you are looking for, please email...

Custom error pages

To create a custom error page for your account you will need to upload a file with the...

How do I use SSL to setup my email?

Set your mail server to the host name for SMTP/POP3/IMAP (instead of domain.com) and then make...