Unable to compile Servlet

Unable to compile Servlet

First I installed the tomcat server in C:\apache-tomcat-6.0.14 directory.

Then I created a system variable CATALINA_HOME with the value C:\apache-tomcat-6.0.14.

Then I created another system variable JAVA_HOME with the value C:\Program Files\Java\jdk1.6.0.14

Then I created another environment variable CLASSPATH with the value C:\apache-tomcat-6.0.14\lib\servlet-api.jar

Then I created my servlet file Servlet_LifeCycle.java under the folder C:\apache-tomcat-6.0.14\webapps\demo-examples. The demo-examples folder was created by me.

To compile my servlet program I did the following:

C:\apache-tomcat-6.0.14\webapps\demo-examples>javac Servlet_LifeCycle.java

But the compilation failed totally.

View Answers

January 25, 2011 at 4:44 PM

Hi Friend,

Follow these steps:

1)create a servlet.

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

public class HelloWorld extends HttpServlet{ 
  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                   throws ServletException,IOException{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<html>");
    pw.println("<head><title>Hello World</title></title>");
    pw.println("<body>");
    pw.println("<h1>Hello World</h1>");
    pw.println("</body></html>");
  }
}

2)Go to the webapps folder of your apache tomcat and create a web application folder but it should having an appropriate name like examples.

3)Create web.xml and classes folder inside the WEB_INF folder of web application folder assuming examples.

4)Copy the servlet to the classes folder.

5)Edit the web.xml to include servlet?s name and url pattern.

 <servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>HelloWorld</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Hello</servlet-name>
 <url-pattern>/HelloWorld</url-pattern>
 </servlet-mapping>

6)Compile your servlet.

7)Run Tomcat server by clicking the startup.bat file. This is located inside the bin folder of apache tomcat.

8)Open the browser and type the following url:

http://localhost:8080/webapplicationfoldername/servletname

For more information, visit the following link:

http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml

Thanks

Ads









Related Tutorials/Questions & Answers:
Unable to compile Servlet
Unable to compile Servlet  First I installed the tomcat server in C... was created by me. To compile my servlet program I did the following: C:\apache...\servlet-api.jar Then I created my servlet file Servlet_LifeCycle.java under
unable to run servlet
-mapping> 6)Compile your servlet. 7)Run Tomcat server by clicking...-pattern>/HelloWorld</url-pattern> </servlet-mapping> 6)Compile your...-mapping> 6)Compile your servlet. 7)Run Tomcat server by clicking the startup.bat
Advertisements
how to compile and run servlet program
how to compile and run servlet program   hello sir/mam i hve installed tomcat5.5 version and also have jdk1.6.0_14 installed but not able to run it or compile i m doing it first tyme pls help me in sorting out this problem. i hve
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to display table data on JSP page that is coming from mysql and servlet.
unable to display table data on JSP page that is coming from mysql and servlet.  I am unable to show table data on JSP page using servlet and mysql. only two rows data i showing but in my database I have five fields
unable to retrive the data from mysql using servlet using jdbc driver
unable to retrive the data from mysql using servlet using jdbc driver  Hi, i have a servlet program..to verify the login and redirect to correct...(request,response); } } .i am unable to redirect..here is the code..pls
compile
compile  how to compile .class files using eclipse
compile
compile  how to compile a java program using jre7
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL  <html> <head> <title>online exam</title> </head> <body> <div
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL
how to access radio buttons selected in a servlet??...iam unable to retrieve values...it is returning NULL  <html> <head> <title>online exam</title> </head> <body> <div
unable to display image using html tag in servlet.(image src is in a variable.....). i am using netbeans IDE. plz..........do help
unable to display image using html tag in servlet.(image src is in a variable.....). i am using netbeans IDE. plz..........do help  import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException
Servlet
-pattern> </servlet-mapping> </web-app> 6)Compile your servlet. 7...Servlet  I want to know the steps to write a simple servlet program... .   Hello Friend, Follow these steps: Put servlet-api.jar inside
unable to execute the examples
unable to execute the examples  unable to execute the examples given for struts,ejb,or spring tutorials.Please help I am a beginner
Servlet
Servlet  how to navigate one servlet page to another servlet page
compile error
compile error  Hello All for example public class A { static void main(String args[]) { System.out.println("Hello World... program with Test.java and try to compile with javac test.java an error like
servlet
servlet  is there any way to include pdf's in servlet
unable to open the service tomcat5
unable to open the service tomcat5  While trying to run tomcat server getting "unable to open the service tomcat5" error after installed tomcat
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
JAVA Compile Question ?
JAVA Compile Question ?  I'm unable to compile this file and not sure why? Can someone please help? import java.nio.file.*; import java.nio.file.attribute.*; import java.nio.file.attribute.Attributes; import
unable to connect database in java
unable to connect database in java  Hello Everyone! i was trying to connect database with my application by using java but i am unable to connect... created. 6) Restart your compiler and compile your java code. For Inserting Data
servlet
servlet  how to interact with a servlet from a swing program
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  i want to create a login page with servlet using database mysql? only in servlet not in jsp plzz help me out
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet hibernate - Hibernate
servlet hibernate  i store the data in database using servlet and hibernate.but unable to retrive plz help me
Servlet
override Service method in the servlet when you extend GenericServlet to create servlet as it is mandatory to override it. But, when you extend HttpServlet to create a servlet then you can't override service method as there is a need to override
servlet
servlet  i want a program for counting the no of times the servlet has been invoked
servlet
servlet  how to create a login form using servlet using submit,edit delete button
servlet
servlet  dear sir servlet and html not run on eclips plz help me

Ads