File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes compiling and deploying into Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "compiling and deploying into Tomcat" Watch "compiling and deploying into Tomcat" New topic
Author

compiling and deploying into Tomcat

Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
How can I compile and deploy a servlet into Tomcat? I tried a lot and gave up. First of all how can I compile using javac?
deepthi Ragunathan
Greenhorn

Joined: Nov 12, 2005
Posts: 6
Originally posted by Alex George:
How can I compile and deploy a servlet into Tomcat? I tried a lot and gave up. First of all how can I compile using javac?


i'll give u an example

let us say the servlet u have written is FirstServlet.java and ur working directory is presently in the d directory.

the file structure of ur webapp should be in the manner shown below

d:\MyApp\
under MyApp u have to place ur FirstServlet.java and and create a
deepthi Ragunathan
Greenhorn

Joined: Nov 12, 2005
Posts: 6
(cont....)

Under MyApps place ur FirstServlet.java and also create a folder WEB-INF

in WEB-INF folder create two more folders lib and classes

Now compile ur java code as shown below

d:\MyApp>javac -d ./WEB-INF/classes FirstServlet.java.

so now the class file of FirstServlet.java will be placed in the classes folder

then provide the necessary info abut your servlet in the web.xml file present in the WEB-INF folder.

now copy the folder MyApps and paste it in webapps folder of tomcat.This is deployment
for eg:
"D:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps"

that's it

now type http://localhost:8080/MyApp/<the url pattern of ur servlet provided in web.xml>

it shld work
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Alex George:
How can I compile and deploy a servlet into Tomcat? I tried a lot and gave up. First of all how can I compile using javac?


Glad to see you're trying again.
I would recommend stepping back from servlets a bit and just finding a book or online tutorial on Java itself. There are many good tutorials out there.
http://www.google.com/search?hl=en&q=JAVA+TUTORIAL&btnG=Google+Search

Once you've become comfortable writing, compiling, and running some simple "Hello, World" programs, and have the concept of classpaths and packages under your belt, working with servlets will be a lot easier, less frustrating, and less time consuming.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
The problem is it complains about the javax package.
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
My classpath pointing to
C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar
May be I am pointing to the wrong place??
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, this underscores my point.
There are some dependencies that need to be on your classpath in order to compile servlets. If you're using a recent version of tomcat, the classes your servlet depends on are contained within the tomcat/common/lib/serlet-api.jar file.

See: http://faq.javaranch.com/view?CompilingServlets
nebula kodatham
Greenhorn

Joined: Nov 10, 2005
Posts: 7
Originally posted by Alex George:
My classpath pointing to
C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar
May be I am pointing to the wrong place??



Copy the servlet-api.jar from <C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar> and place it in ur working directory

then from ur current working directory set the CLASSPATH as shown below:

set CLASSPATH=%CLASSPATH%;.;servlet-api.jar;
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
I am able to compile it. I compiled it and moved it into /path../Tomcat 5.0\webapps\Ch1\WEB-INF\Classes.
I have the following in my web.xml
<web-app>
<display-name>Examples</display-name>
<description>Examples</description>

<servlet>
<servlet-name>Chapter1 Servlet</servlet-name>
<servlet-class>Ch1Servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Chapter1 Servlet</servlet-name>
<url-pattern>/Ch1Servlet</url-pattern>
</servlet-mapping>
</web-app>

But when I go to http://localhost:8080/Ch1/Ch1Servlet
I can not see my servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Copy the servlet-api.jar from <C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar> and place it in ur working directory


Bad suggestion. It's much better to reference the jar file where it lies. Copying jars all over the place will only lead to incompatible versions and frustration.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

deepthi and nebula,

JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

Please read this for more information.

thanks,
bear
Forum Bartender
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
I am sorry to see where this is going. How can I get a real suggestion. There is something wrong going on when I deploy it. Can some body step in?
Priyanka reddy
Greenhorn

Joined: Nov 13, 2005
Posts: 5
hi,

Can you post the code of your servlet you have written???
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Ch1Servlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{

java.util.Date today=new java.util.Date();
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>Time is! "+today+" </h1>");
out.println("</body>");

out.println("</html>");

}
}
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Ok Alex.

Here is a free book: Servlet & JSP

It is not considered a good book for beginners but it would help you in compiling servlet.
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
Another thing I want to tell everybody is that I am able to compile and deploy it on Weblogic. It is just something with Tomcat? Any clues
Priyanka reddy
Greenhorn

Joined: Nov 13, 2005
Posts: 5
Originally posted by Alex George:
Another thing I want to tell everybody is that I am able to compile and deploy it on Weblogic. It is just something with Tomcat? Any clues


your code is working fine in my tomcat server......the output i got is

Time is! Mon Nov 14 21:21:21 GMT+05:30 2005.

were you able to compile your java program
are you getting any errors when you deploy your application???
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
Good to know that my code is working fine in your Tomcat server.
I am able to compile.
So there is something wrong in the way I deploy it. I basically created the following directory structure under webapp:
ch1/WEB-INF/src
/classes

I have web.xml in the WEB-INF folder.
I compiled the source code outside Tomcat and copy the class file into /classes.
Then restarted Tomcat.
Is there another file to set the context root? May be I should set the context root to "ch1", which I did when I deployed the servlet into Weblogic.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Alex George:
How can I get a real suggestion.


You could start by posting all the relevant details.

What version of tomcat have you installed?
What version of Java are you using?
Can you see the tomcat welcome page when you go to http://localhost:8080

But when I go to http://localhost:8080/Ch1/Ch1Servlet
I can not see my servlet.

What do you see?
Have disabled "Friendly HTTP Errors" in your browser so you can see the actual message from the server?

Have you gone through the tomcat logs (located in tomcat/logs) to see what errors if any the container has printed there?
[ November 14, 2005: Message edited by: Ben Souther ]
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
I have another index.html page, which I can see when I go to http://localhost:8080. This page is under webapp/ROOT.
I have Tomcat5.0.28
My java version is 1.4 or later (well I think I have a couple of versions. Is that a problem?).
Given below is the error I am getting when I go to http://localhost:8080/Ch1/Ch1Servlet

type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Wrapper cannot find servlet class Ch1Servlet or a class it depends on org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118) org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705) org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577) org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683) java.lang.Thread.run(Thread.java:534)
root cause
java.lang.ClassNotFoundException: Ch1Servlet org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118) org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577) org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683) java.lang.Thread.run(Thread.java:534)

I have one qustion. How does Tomcat know my context root is ch1?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I am able to compile it. I compiled it and moved it into /path../Tomcat 5.0\webapps\Ch1\WEB-INF\Classes.


Did you really spell "Classes" with an upper case "C"?
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
I think it was "classes" small c.
Is that a problem?
It is "classes" in my Weblogic server. It has to be "Classes" in Tomcat?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, it's "WEB-INF/classes".
And, yes, it would be a problem if it were "Classes".
[ November 14, 2005: Message edited by: Ben Souther ]
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Originally posted by Alex George:

Given below is the error I am getting when I go to http://localhost:8080/Ch1/Ch1Servlet
...
I have one qustion. How does Tomcat know my context root is ch1?


The context root is listed in the url. In the url you have above, the context root is "Ch1" (note the upper case "C"). Try http://localhost:8080/ch1/Ch1Servlet and see if it makes a difference.
[ November 14, 2005: Message edited by: Paul Bourdeaux ]

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
Thank you so much Ben. It was because of the capital letter in �Classes�. I changed it to �classes� and it worked. I spend so much time on this and I got it solved in the end.
And NO it was not because of the capital �C� in Ch1.
My question about context root is that I set context root in Weblogic server in application.xml. Is there any file to set the context root in Tomcat? Or what ever under webapps is the default context root?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are several ways to deploy applications with Tomcat.
The simplest is to drop a war file or properly laid out directory structure into Tomcat's webapps directory.

If you're going to develop with Tomcat I would recommend reading their documentation. All of this is covered there.
http://tomcat.apache.org/tomcat-5.5-doc/index.html
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
How can I get a real suggestion... Can some body step in?
And NO it was not because of the capital �C� in Ch1.

Ok... I was just brainstorming ideas because everything else looked right (your earlier post indicated that classes was lower case). Just FYI, ranchers here will be a lot more prone to offering you advice if you are a little less critical of the help that is offered.

At any rate, I'm glad you got it running.
Alex George
Greenhorn

Joined: Oct 31, 2005
Posts: 22
Sorry about saying c was small letter. "c" was small letter in my Weblogic server and I didn't have access to the machine I have Tomcat. I thought I have the exact same name and structure on both of the machines(I wanted to have it that way). I looked at the file name on Weblogic and it was small c. So I said "small c".
Thank you so much for finding it out.
Anyway would you prefer one server over other?
Would I be able to learn ejbs, jdbc...etc on Tomcat
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Alex George:
Would I be able to learn ejbs, jdbc...etc on Tomcat


JDBC is no problem, but Tomcat does not support EJB's. JBoss does.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Alex George:
Sorry about saying c was small letter.


I think Paul's point had less to do with your typo ("C" instead of "c") and more to do with your tone in dealing with the people who were taking time out of their busy schedules to try to help you.

"How can I get a real suggestion..."
This question suggests that all of the help you have recieved so far was not real or meaningful.

"Can some body step in?"
This one suggests that the people who have been helping you thusfar are not capable and that you would like someone more competent to help you.

"And NO it was not because of the capital �C� in Ch1."
Typing in all caps is the equivalent to yelling on boards.
Where your problem was the result of sloppy typing and where your posts were inconsistent in respect to case, it was perfectly legitimate for someone to suggest that this could be the issue. If you didn't intend to sound like you were barking back at the person who made this suggestion, it was a bad idea to type "NO" in that context.

I considered walking away from this thread when that post showed up too.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Originally posted by Alex George:
I am sorry to see where this is going. How can I get a real suggestion. There is something wrong going on when I deploy it. Can some body step in?


Maybe a little harsh Alex? I understand that people get frustrated, but realize that everyone who posts is making a real suggestion. Just because it might not completely help you, doesn't give you the right to lash out at people. I would rather see a post like.

"Thank you for your suggestions. Unfortunately, I haven't solved the problem yet. Any other ideas?"

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compiling and deploying into Tomcat