This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes tomcat newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "tomcat newbie " Watch "tomcat newbie " New topic
Author

tomcat newbie

Yong Ming Wai
Greenhorn

Joined: Aug 31, 2004
Posts: 12
hie there..

i am new to JSP and tomcat...
what i understand about JSP is it used to display the output while the servlet will do the processing.

Here is basically what i wanna implement in my web application..

user->JSP(form)->pass the value to servlet(.java)->processing value(servlet)->pass the output to JSP->display out(JSP).

i know that all JSP files should put in ../tomcat/webapps/ROOT/
but how about the servlet?...i tried putting it in ../ROOT/WEB-INF/classes
but it gives me errors...

i check in Internet..and most of them told me that i should set the classpath of the servlet the i wanna use..i tried but still give me errors.

anyone?


alloyion
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
The servlet goes into WEB-INF/classes.
No need to tinker with the classpath.
Basically, it your servlet is called "pack.MyServlet" (where "pack" is the package name), try the following:

1. Compile the servlet and put it into "classes", so that you get:
WEB-INF/classes/pack/MyServlet.class
(Pitfalls: make sure to maintain the "pack" directory, and also note that it's the compiled ".class", not the ".java" source).

2. Locate the file "WEB-INF/web.xml" , and add a "servlet-mapping" to it.
A simple "web.xml" looks like:


In your case, if you use the existing ROOT, you'll need to add those entries to the existing web.xml, which already has some entries.
Just note the order: first all "servelt" tags, then all "servlet-mappings", so it'll be something like:
< servlet > ...
< servlet > ...
< servlet-mapping > ...
< servlet-mapping > ...


3. Start up tomcat (to be on the safe side, make sure Tomcat's not running while you do the above changes. Start it only after everything's ready. It's an overkill, but it'll do for starters).

4. Point you browser to:
http://localhost:8080/MyServlet

Hope this helps... if not, could you please quote the exact error message issued by tomcat ?
Yong Ming Wai
Greenhorn

Joined: Aug 31, 2004
Posts: 12
Thanks for advice...

After few changes..i managed to get it done.
Unfortunately,new problems arise along..
My system has alot of folders which contain lots of servlets..(.class)
do i have to declare all of them and map it in web.xml one by one..

examples:

my login.jsp will post value to login.class(login folder) which will import util.class(in system folder) to use DCconnect() to verify the login..then..if valid..
login.class will call welcome.jsp

do i have to..
write...
< servlet > ...
< servlet > ...
< servlet-mapping > ...
< servlet-mapping > ... for all the servlet?
if kinda of troublesome,rite?
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Good question...
I know of 2 workarounds for it (but there could be more):

1. Tomcat is supposed to have an "invoker servlet".
Haven't used it much, but you should be allowed to add the following to "web.xml":


You'll probably find it ugly (lots of if's inside the servlet, and no obvious gain), but it's worth mentioning since that's the idea behind the popular Struts framework (of course, Struts is much smarter than a bunch of silly if's... on the other hand, it still makes you do lots of XML configuration).

Good luck
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I wouldn't steer new users into the using the invoker servlet.
It's been deprecated for a a reason. See: http://faq.javaranch.com/view?InvokerServlet

It's own creators call it evil: http://jakarta.apache.org/tomcat/faq/misc.html#evil

Take the time to map your servlets. It only needs to be done once for each servlet and the benefits far outweigh the hassle of doing so.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: tomcat newbie