To start Tomcat:
view => Runtime
Expand the 'Server Registry' , 'Installed servers' , 'Tomcat' ,
'localhost:8081' , 'defaultcontext' nodes.
Set the default context to where ever you saved your 'HelloWorld'
servlet by right clicking and selecting 'properties' then changing the
value of the 'docbase' attribute (This sets up the server so it knows
where the server root is).
In your docbase folder you should a set up a folder structure as
follows:
WEB-INF/classes
which is where Tomcat will look for your compiled class, your servlet
should go inside the classes folder, press F9 to compile inside of
NetBeans, you should now have a 'HelloWorld.class' file in your
'classes' folder.
In the folder 'WEB-INF' create a file called web.xml (this will tell
the server where your servlet is) and enter the following:
************* START COPY AND PASTE *******************
<?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>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
************* END COPY AND PASTE *******************
Without the copy and paste instruction, save it in the WEB-INF (should
be all uppercase) and call it web.xml
Go back to NetBeans right click on 'Internal Tomcat' and select
'Start'.
Open a web browser and type:
http://127.0.0.1:8081/hello
Note that in NetBeans the default port for Tomcat is 8081, also the
/hello part of that URL is defined in the web.xml file <url-pattern>
node. You should see your HelloWorld servlet.
The servlet I used to test these instructions is below (taken from
http://www.fluffycat.com/java/JavaNotes-HWServlet.html):
************* START COPY AND PASTE *******************
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>HelloWorldServlet</title></head>");
out.println("<body><h1>HelloWorldServlet</h1></body>");
out.println("</html>");
}
}
************* END COPY AND PASTE *******************
Good luck !
Gav
Gavin Donald
http://www.prodia.co.uk
gav AT prodia DOT co 'DELETE THIS' DOT uk