• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to compile servlet from cmd  RSS feed

 
Sagar S Shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys i am completely new to this servlets stuff and am reading it from Head first servlets and JSP.
Currently i have the servlet code ready.
The book uses command prompt for all compiling,etc but i have always used eclipse till date.But as i am new to this stuff this time i decided to do everything as the book says as i have no idea how to do all the servlets stuff in eclipse.

Servlet code:


I am using tomcat 7.

Here is how the book says i should compile:
http://screenshooter...1286718/wqlpofg

I tried it and this is what i get:
http://screenshooter...1286718/ojcmuqi

I am completely lost here.How to solve this problem??
Also is there any easy way of doing it through eclipse???

Thanks in advance.
 
Pyla Rao
Ranch Hand
Posts: 51
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before we are running servlets we need to do many things. here the following example helps a lot for running servlets from command prompt.


Creating a Servlet without any IDE
I want to show how to create a simple servlet without using any IDE like NetBeans or Eclipse. I will use Tomcat 6.0 and Notepad to create a simple test servlet and run it.

The article assumes, you have JDK 5.0/6.0 and Tomcat 6.0.

The following are the steps to be taken by you to create a simple servlet.

Download Tomcat 6.0 from http://tomcat.apache.org
Install Tomcat 6.0 into c:\ApacheTomcat6.0
Go to webapps directory of Tomcat and create the following directory structure for demo application. Any directory placed with required structure of Java EE web application is treated as a web application by Tomcat.




Creating Servlet Source Code
The following is the code for TestServlet.java. It simply sends a message to browser.



Deployment Descriptor - web.xml
Create web.xml as follows. It is better you copy web.xml from some other application in Tomcat webapps directory instead of typing it from scratch. Make sure it is placed in WEB-INF (in uppercase) folder of demo application with the following content.



TestServlet is associated with url pattern /test in web.xml. From the client, we have to make request for url pattern /test.
Compiling Servlet
Go to classes directory, where .java file is placed as enter the following commands at the command prompt to compile the servlet.

path=d:\jdk1.6.0\bin
set classpath=.;c:\apachetomcat6.0\lib\servlet-api.jar
javac TestServlet.java


Make sure you change JDK and Tomcat directories according to your installation.

Set path to the directory where JDK is installed. Servlet-api.jar contains API related to servlet like HttpServlet, HttpServletRequest etc., so it must be placed in the classpath. Compile the servlet (.java) to create .class file.

Starting Tomcat and Running Servlet
Start Tomcat by taking the follwing steps from BIN directory of Tomcat.

set java_home=d:\jdk1.6.0
startup


Once, tomcat is successfully started, go to browser and enterer the following URL.

http://localhost:8080/demo/test



 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umamahesh pyla welcome to the Ranch
Since you are new, I have added code tags to your post, and you can see how much better it looks Please avoid very long lines in code; the left-to-right scrolling makes them difficult to read.
 
Sagar S Shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks a lot.I did all the steps as you said but this is the error i get:


http://screenshooter.net/1286718/akhqdli
 
Sagar S Shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys problem solved.
Tbh i really didnt completely understand how i solved it.i have been trying for hours and somehow its working now..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!