Simple steps to deploy on tomcat server
Hi, this post explains how to deploy a simple servlet on tomcat server.
Prerequisites: Should have tomcat installed. should have jvm installed, should know how to set classpath from command prompt and know how to compile a .java file.
(to know how to set classpath, refer: http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html)
I am using apache-tomcat-6.0.29.
(Assuming the server is not presently running)
1. Locate webapps folder in tomcat's installation directory.
2.In that folder, create a new folder "HelloWeb".
WARNING!!!!!!: While copy pasting the source code from the web page to notepad file, the double quote characters kind of get corrupted. So, after copy pasting in notepad from the webpage, remove the double quote(at each place,even in the headers of html and xml files) and reenter the double quote at the palce where you removed it. you can notice significant difference b/n the double quote character before and after. That is, web page double quote is not same as notepad double quote. We need notepad double quotes if we want our code to run (whether it's .xml file or .java file or .html file).
3.In that HelloWeb, create a file called index.html and copy paste the following content.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
<form action="http://localhost:8080/HelloWeb/chat" method="POST" >
<input type="text" size="60" name="chattext"Â />
<input type="submit" value="submit" />
</form>
</body>
</html>
4. Create a new folder called "WEB-INF" in "HelloWeb".
5. In "WEB-INF" create 2 folders: "classes" and "lib"
6. Compile the Chatservlet.java in command prompt. Here is the source code for Chatservlet class.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Chatservlet extends HttpServlet{
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
 {
  System.out.println(request.getParameter("chattext"));
 }
(Note: while compiling the above source code from command prompt, you will need servlet-api.jar file. It is available for download at:http://www.java2s.com/Code/Jar/STUVWXYZ/servlet-api.jar.htm. Include this jar file in classpath before compiling the above code)
7.Copy-Paste the Chatservlet.class file in "classes" folder of "WEB-INF" folder.
8. In "WEB-INF" folder, create a file called "web.xml" and paste the following content in it:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>HelloWeb</display-name>
 <welcome-file-list>
   <welcome-file>index.html</welcome-file>
   <welcome-file>index.htm</welcome-file>
   <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
 <servlet-name>chat</servlet-name>
 <servlet-class>Chatservlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>chat</servlet-name>
 <url-pattern>/chat</url-pattern>
 </servlet-mapping>
</web-app>
9.You can leave the "lib" folder in "WEB-INF" folder empty for this application.
10.now, start the server and access your application through browser by this URL http://localhost:8080/HelloWeb/
Any thing that you enter in the textbox will be printed on the server console.
Any doubts, feel free to ask.