"HelloWorld" Servlet with Tomcat Web server
Installing Apache Tomcat
-
Extract and move it to appropriate folder.
Go to [your tomcat]/bin and run: startup.sh
-
Back to [your tomcat]/bin and run: shutdown.sh
Refresh your browser to check whether your tomcat was stopped.
Creating "HelloWorld" servlet
Create a folder named “hello”: mkdir hello.
Then create a folder named
WEB-INF inside hello, and a folder named
classes inside WEB-INF:
cd hello
mkdir WEB-INF
cd WEB-INF
mkdir classes
Create a Java class in the
WEB-INF/classes folder, named it
HelloServlet.java and insert the following source codes:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<HTML>");
out.println("<BODY>");
out.println("Hello World !!!");
out.println("</HTML>");
out.println("</BODY>");
}
}
Compile the
HelloServlet.java by executing:
javac -cp [your tomcat]/lib/servlet-api.jar HelloServlet.java
You will get a file HelloServlet.class in the same folder.
In folder
WEB-INF, create a file
web.xml with the following content
<web-app>
<servlet>
<servlet-name>HelloServletName</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServletName</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
Deploying the "HelloWorld" servlet
Copy the folder hello to [your tomcat]/webapps folder.
Start your Tomcat. (if your Tomcat is running, your don't need to restart it because it updates applications dynamically)
-
Exercises
Add the path “[your tomcat]/lib/servlet-api.jar” to your CLASSPATH environment variable so that when compiling a servlet project, you do not need to declare the library [your tomcat]/lib/servlet-api.jar in the command line. Step 4, part “Creating HelloWorld servlet” would become javac HelloServlet.java.
Modify the HelloWorld servlet above to return a random number instead of the string “Hello World!!!”. Compile and check the result.
Create another servlet that return
a list of student names in
HTML.
Modify the servlet in exercise 3 to return the list in XML.
Back to top