• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets and Package Membership

 
Joshua Lam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its seems that just about all the servlet books and articles I read uses servlets that below to the 'default' package. ie these examples do not begin with:
package mypackage; // *** this is not often used
...
public class CoolServlet extends HttpServlet
{
...
}
These servlets are then installed somewhere on a server's 'servlet' directory.
Is there are reason for this? I always thought that it is good practice to put classes in packages, in this way, there is less room for ambiguity and it is more secure.
Is it possible to write a servlet that has a user defined package membership? Is so, how is it invoked?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is possible, and useful. Most of the servlets I produce are in packages. The only thing to beware of is that there seems to be no way (that I have found) of accessing classes in the default package from a class in a named package, so if you need to use other default-package classes, you may end up having to put your servlet into the default package as well.
How such servlets are installed or invoked depends on the web server/servlet engine, but typically there is some way of associating a URL or name with a fully qualified class name. The documentation for your server/engine should tell you how.
Alternatively, a lot of servers also allow the use of a fully qualified class name in the URL if you just want to put the servlet into the monitored directory. eg.
The server looks at:
/something/blah/servlets
you put your classes in
/something/blah/servlets/com/you/project/MyBusinessLogic.class
/something/blah/servlets/com/you/project/MyServlet.class
Then you might be able to refer to your servlet as
http://www.you.com/servlets/com.you.project.MyServlet
This is obviously a bit messy, so associating a simpler name with the servlet is the usual way to do it.
 
Joshua Lam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. There is very helpful.
One more question. Is it possible to deploy servlets with jar files? The servlet engines I am experiment do not say anything about jar files but just drop my servlet classes into a 'servlet' directory or some user configured directory.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "just drop the classes in" method of servlet installation is usually only provided as a way of getting started with simple servlets. To deploy a servlet from a jar file you need to tell the servlet engine where the jar file is, and maybe add it to a servlet engine classpath. This varies a lot between servlet engines at the moment, although version 2.2 of the servlet specification aims to improve this situation.
Can you let us know which servlet engine(s) you are trying to use? It's very hard to talk in abstract terms about such concrete issues.
 
Joshua Lam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Carver:

Can you let us know which servlet engine(s) you are trying to use? It's very hard to talk in abstract terms about such concrete issues.

Oops, sorry, I am currently evaluating JRUN 2.3.3 on Win NT4/IIS4 environment.
 
Joshua Lam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joshua Lam:
Oops, sorry, I am currently evaluating JRUN 2.3.3 on Win NT4/IIS4 environment.

Actually, just to clarify. I do know how to set the classpath in JRUN to point to a jar file so that servlets using classes in that jar file can know how to load those classes. However, can we put a servlet in that jar file and invoke it from JRUN like other servlet in the 'servlet' directory?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic