• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compiling and deploying into Tomcat

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Alex George
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is it complains about the javax package.
 
Alex George
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Can you post the code of your servlet you have written???
 
Alex George
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>");

}
}
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Alex George
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic