Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

TO RUN SERVLET IN TOMCAT 5.

5 FIRST CREATE THE FOLDER STRUCTURE LIKE BELOW


C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ UNDER WEBAPPS FOLDER \ PROJECT FOLDER NAME \ index.jsp index.html web_inf \ classes web.xml in servlet url pattern

give as /servletname/*

In html form action="servletname" method="get or post" create dsn in system dsn in sql server or any database you have To compile the servlet set the java path set the classpath in cmd prompt

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\s2s\WEBINF\classes>set classpath=C:/Program Files/Apache Software Foundation/Tomcat 5.5/common/lib/servlet-api.jar; press enter javac servletname.java go to Browser http://localhost:8084/ 1.click manager give user and password if u donot know go to tomcat install.directory/ conf/tomcat-users.xml u find there 2.click the project of yours and enjoy running servlet.

How to run First Servlet in Tomcat 5.5


Pre-requestic i am assuming you have already installed jdk 1.5+ and Apache Tomcat 5.5.x if not please visit first How to Install Tomcat and You are familiar with how to compile Servlets if not please visit first How to Compile Servlet STEP-1 webapps is a Folder in location C:\Program Files\Apache Software Foundation\Tomcat 5.5 where java web application resides and run and every Java web Application has a pre-defined structure (specified by Java), every WebServer(eg- Tomcat ) and Application Server(Which has Servlet-container) has to Follow theses Specification. Structure of Web Application is Shown Below.

STEP-2 How to Make your first java web Application go Inside Webapps Folder and copy the Root Folder(it is default web application) copy it and paste it in webapps folder,its will create folder copy of Root folder Rename it (eg- F2 Key) and type name delhi . Go inside delhi and delete all files and images except WEB-INF folder.

Go Inside WEB-INF Folder, it Contains only web.xml file ,create folder named classes (no capital letter & no spaces) Copy your Helloworld.class file in this classes Folder. STEP-3 Open web.xml in any Editor (eg- Notepad or EditPlus) and Type the Following Code inside <web-app></web-app>Tag. <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> your web.xml Should Looks as Screen Shown below.

STEP- 4 Start Tomcat 5 & type http://localhost:8080/delhi/hello in Browser Window .

Hello World will be Shown First

Congrats you have Successfully Run your

How to Compile First Servlet


1- Type the Following Code Of HelloWorld.java and Save in a Bin Folder Of JDK1.5 . Source Code for HelloWorld Example import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld 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(<head>); out.println(<title>Hello World!</title>); out.println(</head>); out.println(<body>); out.println(<h1>Hello World!</h1>); out.println(</body>); out.println(</html>); } } 2- To Compile this HelloWorld.java and change Directory to eg- c:\jdk1.5_10\bin (your java installation directory might be change) i have installed tomcat in following location C:\Program Files\Apache Software Foundation\Tomcat 5.5 ,goto Command Prompt

To Compile Helloworld.java Prompt ,Type the Following command at Command

C:\jdk1.5.0_10\bin>javac -classpath C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar HelloWorld.java

You might also like