Home Servlets Working with Tomcat Server
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Working with Tomcat Server

Advertisement
Apache Tomcat is a famous Servlet container developed at Apache Software Foundation.

Working with Tomcat Server

     

This section explains how to work with Tomcat Server

Apache Tomcat is a famous Servlet container developed at  Apache Software  Foundation. This software is released under under the Apache Software License. Anyone can use it for the development as well as deployment of the applications. Tomcat is the official reference of implementation of java  Servlets and java  Server Pages. Tomcat is very easy to install and configure.  Anyone can learn it very fast and start using the Tomcat server for the development and deployment of the web applications. 

These days many web hosting companies are providing Tomcat support on their server. So, if you develop the application in Java technology you can get any host and then deploy it on the internet. Earlier it was a difficult task to get a good host for hosting.

So, let's get started with the Tomcat server.

1) Installation and Configuration- Apache Tomcat  Server is free software available for download at www.apache.org and www.jakarta.apache.org.The current version of tomcat server is 6.0. This server supports Java Servlets  2.5  and Java server Pages  2.2. JDK is also free software available for download at www.java.sun.com. Its current version is 6.0. 

Learn Tomcat Installation in detail. You will also find the description on how to run example with tomcat.

Deploying Servlets On  Tomcat  Server

To  deploy servlets on  Tomcat  Server, following steps are to be taken for example given  below.

1. Create web application

To develop an application using  servlets or jsp, a directory structure  is  to be maintained for the example given  below.

Step1: Create a web application folder (servlet-examples) under tomcat webapps directory.  The  path will be C:\apache tomcat\webapps\servlets-examples.

Step2:  Create a WEB-INF folder which should be created under servlets-examples.

Step3:  Create  web.xml file and classes folder under the WEB_INF folder.

2.  Compile the servlet Program- Create a servlet program and compile it on the command Prompt .The procedure is not different from any java  program. The set of classes required for writing servlets is available in  servlet-api.jar which is put into CLASSPATH.

3.  Copy the Servlet class(Hello) into classes folder, which is under WEB-INF folder.

4.  Edit web.xml to include  servlet's name  and url pattern.

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>

5.  Run Tomcat Server and execute  your servlet-   To run the server Go to C:\apache tomcat\bin\startup.bat and double  click on it , the server will start up. After assuring that  the server is running successfully, you can run your servlet. To  execute your servlet,  open  your web browser and  type the url which you have mentioned in your web.xml. The url will be like this:

http://localhost:8080/servlets-examples/Hello

Let's see an example Servlet "Hello". Here is the code of "Hello.java".

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello 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("hello"); 
out.println("</body>");
out.println("</html>");
}
}

Output of program:

Download Source Code

Advertisement

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: May 24, 2009

Related Tutorials

Ask Questions?    Discuss: Working with Tomcat Server   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Danilo
March 22, 2012
LZzWxdaMzpUPh

I am using the device 3530 and i am inafcg the same problem. My application is not able to communicate with my server. for making Http Connecdtion i use the following properties.conn.setRequestProperty( User-Agent , Profile/MIDP-1.0 Configuration/CLDC-1.0 );conn.setRequestProperty( Content-Language , en-US );conn.setRequestProperty( Content-Type , application/binary );conn.setRequestProperty( Accept , application/binary );conn.setRequestProperty( Connection , close );conn.setRequestMethod(HttpConnection.POST);conn.setRequestProperty( Content-Length , Integer.toString(data.length));but i am not able to access the server to which i am pointing.Thre response code give me the HTTP_BAD_GATEWAY : error code 502. what it means and how it can be solved? can you give me the solution for that.??
jignesh
September 6, 2012
about tutorial

best tutorial ever!!!!!!!!!!
DMCA.com