Servlet classes directory

P

paolo

Hello

All the classfiles have to be in the directory WEB-INF/classes/,
right? What if you want to add a directory like
WEB-INF/classes/underDirectory/ ? If I do this, and put
MyServlet.class in this directory, how can i reach this servlet from a
browser? I tried the most logical:
http://localhost:8080/servlet/underDirectory/MyServlet
but i got "The page cannot be displayed". Why? Can't you put class
files in other underdirectory, or what?

Paolo
 
T

Truls Thirud

paolo said:
Hello

All the classfiles have to be in the directory WEB-INF/classes/,
right? What if you want to add a directory like
WEB-INF/classes/underDirectory/ ? If I do this, and put
MyServlet.class in this directory, how can i reach this servlet from a
browser? I tried the most logical:
http://localhost:8080/servlet/underDirectory/MyServlet
but i got "The page cannot be displayed". Why? Can't you put class
files in other underdirectory, or what?

Paolo

You can use any directory structure you like under WEB-INF/classes - each
directory corresponds to a java package. Use packages to organize your java
classes -- see http://java.sun.com/docs/books/tutorial/java/interpack/ or
any other java intro text.

If 1) your servlet MyServlet has been declared to be in package
"underDirectory" ... like in

.....
package underDirectory;
public class MyServlet extends <someServletSuperClass> {
}
....

and 2) you put the MyServlet.class file in the
WEB-INF/classes/underDirectory directory,

then you could use

http://localhost:8080/servlet/underDirectory.MyServlet

to reach your servlet. Note the use of the fully qualified name of the
servlet : "underDirectory.MyServlet".

Good luck!


Truls
 
J

Juha Laiho

(e-mail address removed) (paolo) said:
All the classfiles have to be in the directory WEB-INF/classes/,
right? What if you want to add a directory like
WEB-INF/classes/underDirectory/ ? If I do this, and put
MyServlet.class in this directory, how can i reach this servlet from a
browser? I tried the most logical:
http://localhost:8080/servlet/underDirectory/MyServlet
but i got "The page cannot be displayed". Why? Can't you put class
files in other underdirectory, or what?

As the other response already said, the directory structure under
WEB-INF/classes is modeled by the package structure of your program.

However, there does not need to be any relation between your
package structure and the URL hierarchy - because in the web.xml
you can create arbitary relations between URLs and servlet classes.

It isn't uncommon to have several URLs pointing to a single servlet
class (the class changing its behavior based on the URL via which
it was called), and it's also possible to have some servlets that
aren't accessible by URLs at all (can only be accessed through other
servlets), and so on.

For details, please read the Java Servlet Specification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top