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 Servlets and the fly likes where to put servlets? (searched 1st) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "where to put servlets? (searched 1st)" Watch "where to put servlets? (searched 1st)" New topic
Author

where to put servlets? (searched 1st)

Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
I have enabled the root command:
<Context path="" docBase="ROOT" debug="0"/>

and also enable invoker socket yes aware of exploit)
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>


when placed in:
C:\Tomcat4.1\webapps\examples\WEB-INF\classes

A servlet WILL function by browsing to:

http://localhost:8080/examples/servlet/HelloServlet

(However, that's the EXAMPLE dir)

This webpage
http://www.moreservlets.com/Using-Tomcat-4.html#Test1
said servlets go into:
C:\Tomcat4.1\webapps\ROOT\WEB-INF\classes
(I had to create "classes" dir
and access via:
http://localhost:8080/servlet/HelloServlet

I get 404 not found error - that's why put the files where I knew the example files were - to make sure Tomcat was functioning - appears so.

Tried searching on this, have been unable to find it.
(Yes I know link to Tomcat documentation, but have not been able to find it there either - main page of docs has links to JSP...didn't see to servlets)

Thank you!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

TOMCAT_HOME/webapps/{your-app-name}/WEB-INF/classes/{your-package}/{your-servlet-classes}


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

Joined: Mar 03, 2005
Posts: 20
just about to go try that, wanted to reply and try to catch you...what if you dont' have any packages..thank you!
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
and with that path you can access it from correct...going to try
http://localhost:8080/servlet/HelloServlet
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Depending on the version of the JVM, un-packaged servlets and beans may not be found. I'm not sure which because I always package my classes.

TOMCAT_HOME/webapps/{your-app-name}/WEB-INF/classes/{your-servlet-classes}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Marvin Harrison:
and with that path you can access it from correct...going to try
http://localhost:8080/servlet/HelloServlet


http://localhost:8080/servlet/{your-package}/{your-servlet}

If they're un-packaged, the link you have should be right.
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
TOMCAT_HOME\webapps\HelloServlet\WEB-INF\classes
is where I put the files
HelloServlet.java
HelloServlet.class

http://localhost:8080/servlet/HelloServlet
http://localhost:8080/servlet/HelloServlet/HelloServlet

both pages throw 404 - again it works when placing inside the examples directory.

Is there any setting I need to change inside the server.xml possibly?

Thank you again
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
Tried your 1st example:
http://simple.souther.us/

get 404

message: SimpleServlet/simple-servlet
Description:The requested resource (/SimpleServlet/simple-servlet) is not available.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

TOMCAT_HOME\webapps\HelloServlet\WEB-INF\classes

http://localhost:8080/HelloServlet/servlet/HelloServlet
[ March 10, 2005: Message edited by: Ben Souther ]
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
doh no your program DOES work, deleted the zip
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, the combination of MSIE adding a ".zip" extension and Windows hiding extensions for known file types is always helpful in cases like this.
[ March 10, 2005: Message edited by: Ben Souther ]
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
Tried another random file from your site and it worked as well...very odd.

not familar with making packages as you have yet, but this is just another compoudong factor to learn it...

still drives me nuts that it works in example dir but not in here...VERY ODD
tried your latest link no go
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you restarted Tomcat since you uncommented the invoker servlet?
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20



Thinking about this, i would not be surprised if this code was faulty, alot is from this book I'm on.... Damn
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
yes did it redundantly wondering wtf? hehehe
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is not a whole lot to packaging and you have some working examples now.
If you have no compelling reason to use the the invoker servlet, dump it.
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
yes, and i apprecite your time(i know how valuable it is) -- understand VERY BASIC packaging, but have yet to do anything like you have , just simple "package packname;" etc...

Thank you!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Very basic packaging is enough.

Just create a package called test.

At the top of your classes, include the line:
package test;

Then put your classes in a directory named "test"
and put that directory in your classes directory.

Make sure the path to your "test" directory is on the classpath before compiling.
Marvin Harrison
Greenhorn

Joined: Mar 03, 2005
Posts: 20
just wanted to state for any future searches for others w/same problem...

what I did was take his "simpleservlet" package, and replaced all the simples with hellos...making sure to maintain case sensitivity

additionally, when compiling HelloServlet, just had the package name as "package us;" instead of something long for this quick check.

Sure enough, it worked, and this is with ALL server.xml settings back to default as advised

So, I have no clue why it will work in examples dir, but not here....but really....it's okay, because I just gained (and hopefully future readers) extra knowledge to take packaging skills to the next level above extremely basic.

Thank you VERY MUCH Ben!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
When a servlet works in the examples "web application" directory but nowhere else, it is typically because the examples web.xml contains a mapping for the invoker servlet. For example, this one from Tomcat 4.1.30

where the default web.xml in TOMCAT_HOME/conf has

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where to put servlets? (searched 1st)