Wednesday, 20 October 2010

1. Install JDK ( java development kit ).

2. Install Apache Tomcat (If you oracle installed in ur system then change the port number from 8080 to some other number because oracles default port number is 8080).

3. Once you have installed then you have set the PATH and CLASSPATH. To do this setting right click on My computer> advanced > environmental variables 
click new and set as below:


PATH

C:\Program Files\Java\jdk1.6.0_20\bin;

CLASSPATH

c:\Program Files\Apache Software Foundation\Tomcat 6.0\common\lib\servlet-api.jar

JAVA_HOME

c:\Program Files\Java\jdk1.6.0_20

CATALINA_HOME

c:\Program Files\Apache Software Foundation\Tomcat 5.5

4.  Once you have done with the settings. Then write the servlet program you want to execute.
For Example:

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}


Now save the file name with HelloServlet.java (give the name of class).
Compile the java program and if you compile without any errors, then a .class file is generated.

  • Now go to  C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps and create a folder of your wish.
  • Inside this folder you create a "WEB-INF" folder and inside this "WEB-INF" folder create "classes" folder along with a "web.xml" file as given below: 


NOTE: 
  • Servlet- name can be given any name but the Servlet-class name should be the class name of the servlet program.
  • url-pattern should be same as the name of the servlet-name followed by a slash( / ) as given in the above xml file.
  • If you want to add some Html file to invoke the servlet then do that outside the "WEB-INF" folder.

5. Save the .class file in the "classes" folder. And the web.xml file in the WEB-INF folder.

  • Now C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin and run tomcat server. Then go to firefox or internet explorer and type url as 
                      http://localhost:2010/foldername/servletname
  • Foldername is the name of the folder where you saved ur servlet. And servlet name is the name given in the web.xml file. If everything is correct you will get the output.
6. If u have a html file added to the servlet then go to http://localhost:2010 and then tomcat manager and run ur foldername. Thats it !!!

if u like this post say thanks lol !!!
                                                                                                                                               Written By,
                                                                                                                                              Susheel singh

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube