Tuesday, 22 January 2013

Setting up Apache Tomcat Server for Servlet

Hai Friends Now i am going to share about how we can set up Apache tomcat server for Servlet Program..

Step 1:
First think we have to download Apache Tomcat...
You can go to home site of apache or Click here

Step 2:
Extract the downloaded zip file into C:

 
 Step 3:
create new folder in webapps folder... consider our folder name is "test"


Step 4:
create another folder within "test" folder.. set new folder name as "WEB-INF"






Step 5:
create "classes" folder within "WEB-INF" folder..








Step 6:
create web.xml file within WEB_INF folder.
and copy and paste following code into web.xml file






web.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app>
<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>
</web-app>


here Hello is a servlet class name..
each an every time we have to put entry in web.xml file for every new servlet class..



Step 7:
now we have to start the server..
open
Right click the my computer ->Properties ->Advanced System Settings ->Environment Variables

choose Path..and press edit button..





Step 8:
Copy the Java jdk path upto bin folder location from our computer..
and paste tat url into path text field..




Step 9:
now press "new " button give name as JAVA_HOME
and give java jdk path upto jdk folder...



Step 10:
now press "new " button give name as classpath
and give url of "servlet-api.jar" file.
This file is located on tomcat server's lib folder.





Step 11:
Now press ok in that window.. now move on to apache folder.. in bin folder we will find startup batch file



Step 12:

double click the startup file..now your server has started..



Step 13:
Open Browser window and type "localhost:8080"


Step 14:
It will display tomcat server home page..



Step 15:
Now we will test with Sample Program..

Step 16:
open note pad and copy following code

Hello.java


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Hello extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("Its Working");
}
}









Step 17:
Save this file into classes folder as java file. and compile this file through cmd..













Step 18:
now class file has generated..

Step 19:
now we have to put servlet class name entry in web.xml file..
but we have already contain tat name.. so we no need to put new entry.













Step 20:
now we will run our program through browser
type following url in browser address bar

localhost:8080/test/hello


Step 21:
now it will display output



No comments:

Post a Comment