Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Creating Web application on tomcat server 
 

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

                         

» View all related tutorials
Related Tags: c database security web com rest ant website data io connection user password word uri name using this tab check

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.