Hello all,
I here posts the steps i carried out to execute the
servlet. If anyone have different idea pls post.
Steps to create your directory and work in Tomcat 4.1
1.Create your directory in Tomcat�s webapps
Directory.
�If your tomcat is in d:\ the your directory may look
�D:\tomcat\webapps\<your directory>
�E.g. �d:\tomcat\webapps\test�
2.In the
test directory you can your html files
directly or you can create a directory to hold html
files.
�E.g. �d:\tomcat\webapps\test\first.html (OR)
�E.g. �d:\tomcat\webapps\test\html-files\first.html�
3.Create a folder called WEB-INF inside test
�E.g. �d:\tomcat\webapps\test\WEB-INF
�Under the WEB-INF folder put your web.xml file
4.Create another folder named classes under the same
test
�E.g. �d:\tomcat\webapps\test\classes
�Here you need to place your servlet classes
�E.g.
�d:\tomcat\webapps\test\classes\testServlet.class
5.Start any browser that are
java enabled
6.Type �http://<machine name (or) IP add.
>:8080/<your folder name>/<html file>�
�E.g.
http://localhost:8080/test/ html-files
/first.html
�E.g.
http://127.0.0.1:8080/test/html-files/first.html 7.Click the component (button, label�) to enable
servlet
8.That�s all! Now
you should get your servlet
executed.
Files:
1.web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet/*</url-pattern>
</servlet-mapping>
</web-app>
2. first.html
<html>
<body>
<form method="POST"
action="http://localhost:8080/test/testServlet">
<input type=submit value=click></input>
</form>
</body>
</html>
3. testServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class testServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,
HttpServletResponse res)throws ServletException,
IOException
{
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello! </h1>");
out.println("</body>"+"</html>");
}
}
I have put my first.html inside the folder
�d:\tomcat\webapps\test\html-files\� and testServlet
is under �d:\tomcat\webapps\test\WEB-INF\classes\� folder.