HOW-TO: Create Servlets

J

Justin L. Davidson

HOW-TO: Create Servlets

Step 1: (add servlet mapping in web.xml to class)

--- /usr/local/jakarta-tomcat-4.0.4/webapps/ROOT/WEB-INF/web.xml --

<?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>hall.HelloWorld</servlet-class>
</servlet>
</web-app>


Step 2: (create and compile java class)

-- /usr/local/jakarta-tomcat-4.0.4/webapps/ROOT/WEB-INF/classes/HelloWorld.class
--

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}

Step 3: (restart servlet container)
/usr/local/jakarta-tomcat-4.0.4/bin/shutdown.sh
/usr/local/jakarta-tomcat-4.0.4/bin/startup.sh


Step 4: (test application in browser)

http://justin.ctcnet.com:8080/servlet/HelloWorld



Good Luck!

(e-mail address removed)
 
W

William Brogden

HOW-TO: Create Servlets

Step 1: (add servlet mapping in web.xml to class)

--- /usr/local/jakarta-tomcat-4.0.4/webapps/ROOT/WEB-INF/web.xml --

<?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>hall.HelloWorld</servlet-class>
</servlet>
</web-app>


Step 2: (create and compile java class)

BZZZT - error if the class is in package hall then it must live in
... /classes/hall/HelloWorld.class

--

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}

Step 3: (restart servlet container)
/usr/local/jakarta-tomcat-4.0.4/bin/shutdown.sh
/usr/local/jakarta-tomcat-4.0.4/bin/startup.sh


Step 4: (test application in browser)

http://justin.ctcnet.com:8080/servlet/HelloWorld

BZZZT - error that "/servlet" URL depends on the "invoker" servlet
being turned on. Not true by default since mid Tomcat 4.1
Read this FAQ:

http://faq.javaranch.com/view?InvokerServlet

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top