tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: Servlets directly accessible from browser
Date Sun, 31 Mar 2002 16:02:22 GMT
There are a couple concepts to understand here:

1. unless you provide a mapping to your servlet, the Servlet engine won't 
find it for you.  Servlets are not directly loadable.  Static files are (as 
are .jsp's).  Servlet mappings are set up in the web.xml.  In Tomcat, a 
default servlet mapping is set up for you for convenience: the directory 
"servlet" which comes after your context name.  So, that would be 
http://localhost:8080/test/servlet/.

Now that we know that, this brings us to point #2.

2.  Either you must provide an alias for your servlet or reference it by 
its fully qualified name;  that being the package + classname.  If your 
servlet doesn't specify a package, then it would just be your ClassName.
Given no specified alias and the servlet belonging to package 
com.mycompany.servlets, your Counter servlet would be access like:

http://localhost:8080/test/servlet/com.mycompany.servlets.Counter

and if it doesn't have a package:

http://localhost:8080/test/servlet/Counter

You can provide an alias by adding the following to your web.xml file 
(inside /test/WEB-INF/ directory):

<servlet>
         <servlet-name>counter</servlet-name>
         <servlet-class>com.mycompany.servlets.Counter</servlet-class>
</servlet>

Now you can actually access your servlet like this:

http://localhost:8080/test/servlet/counter

Notice that even though, in this case, the servlet was part of a package, 
you can still access it without referring to the package since you 
specified an alias for the servet.

You can do even more if you add your own mapping:

<servlet-mapping>
         <servlet-name>counter</servlet-name>
         <url-pattern>/counter</url-pattern>
</servlet-mapping>

Now you can access the servlet without using the default mapping 
"/servlet/" since you are providing your own mapping:

http://localhost:8080/test/counter

notice how the "servlet-name" tag within the "servlet-mapping" tag is the 
same name as we specified in "servlet-name" tag within the "servlet" tag.

There are lots more complex examples that you can use by looking at the 
servlet spec or getting a good book on servlets such as Oreilly's Java 
Servlet Programming 2nd Edition, but this should get you started with what 
you need.

Jake



At 07:19 PM 3/31/2002 +0800, you wrote:
>Hi there,
>
>I have already set up a simple context at catalina_home/webapps/test/
>
>I have a few class files there (servlets). I find that I cannot directly
>execute them through the browser (e.g. localhost:8080/test/Counter to run a
>Counter.class servlet).
>
>Is it because of the way I write the servlets or must I add some
>configuration parameters to make all the servlets directly executetable?
>
>Thank you in advance.
>
>Yours truly,
>Hanxue
>
>
>_________________________________________________________
>Do You Yahoo!?
>Get your free @yahoo.com address at http://mail.yahoo.com
>
>
>--
>To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
>For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
>Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message