Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Quickguide Tomcat Creating Web application on tomcat server

Related Tutorials


 
 

Share on Google+Share on Google+

Creating Web application on tomcat server

Advertisement
In this section we will show you how to create first web application using Servlet on the tomcat server. We first make a class named as HelloWorld that extends the abstract HttpServlet class.

Create your first Tomcat Web application

     

In this section we will show you how to create first web application using Servlet on the tomcat server. 

We first make a class named as HelloWorld that extends the abstract HttpServlet class. The code written inside the doGet() method takes two arguments, first one is the reference of HttpServletRequest interface and the second one is the HttpServletResponse interface. This method can throw ServletException. This method calls the getWriter() method of the PrintWriter class. 

Set the classpath of the relevant jar files to compile the servlet i.e.Setting the classpath of the servlet-api.jar file in the variable CLASSPATH inside the environment variable by using the following steps.
 

Now create a java source file as described further in this tutorial and a web.xml file in a directory structure. 

Compile the java source file, put the compiled file (.class file) in the classes folder of your application and deploy the directory of your application in the webapps folder inside the tomcat directory.

The code the program is given below:

Package myservlets;

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("<body>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
out.close();

}

XML File for this program:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd version="2.4">

<description>Examples</description>
<display-name>Examples</display-name>

<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>myservlets.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern> /hello</url-pattern>
</servlet-mapping>
</web-app>

The output of the program is given below:

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 8, 2008

Related Tutorials

Discuss: Creating Web application on tomcat server  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
JPR
March 6, 2013
Minor error corrections

"Package" and "Void" should not be capitalized. "Set the classpath of the relevant jar files to compile the servlet i.e.Setting the classpath of the servlet-api.jar file in the variable CLASSPATH inside the environment variable by using the following steps. " What following steps?! If you have installed Tomcat 7.0.26 in /usr/bin, you can compile with this command: javac -cp /usr/bin/apache-tomcat-7.0.26/lib/servlet-api.jar myservlets/HelloWorld.java
srikanth
December 9, 2013
thanks for simple code

thank you so much for the simple code attached.
DMCA.com