web.xml and mapping

B

B. Stein

Hey to servlet programmers,
using servlets without mapping is running on my SUSE LAMP + TOMCAT
(SLES 8).
Calling my servlet by
http://hostname:8080/my_Servlet_dir/servlet/my_Package.myServlet

works fine, but

is it possible to shorten this path, e.g. by using mapping in web.xml
file?

Can i call my servlet directly by name e.g. http:/hostname/any_Name
if i edit my web.xml like:

<servlet>
<servlet-name>anyName</servlet-name>
<servlet-class>myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anyName</servlet-name>
<url-pattern>/servlet/myPackage/myServlet</url-pattern>
</servlet-mapping>

Please tell me how to specify it in web.xml

Thanks
B. Stein
 
J

Juha Laiho

(e-mail address removed) (B. Stein) said:
Hey to servlet programmers,
using servlets without mapping is running on my SUSE LAMP + TOMCAT
(SLES 8).
Calling my servlet by
http://hostname:8080/my_Servlet_dir/servlet/my_Package.myServlet

works fine, but

is it possible to shorten this path, e.g. by using mapping in web.xml
file?

Can i call my servlet directly by name e.g. http:/hostname/any_Name
if i edit my web.xml like:

<servlet>
<servlet-name>anyName</servlet-name>
<servlet-class>myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anyName</servlet-name>
<url-pattern>/servlet/myPackage/myServlet</url-pattern>
</servlet-mapping>

The servlet-name is abstract; it's purpose is just to connet related
servlet and servlet-mapping elements.

You can do whatever you want to the url-pattern, though -- so you
can set what is the path to the servlet within the namespace of
the context where your application is deployed.

So, you get down to

http://hostname:port/context/yourservlet

This is all you can do with web.xml.

Now, if you deploy your application to the ROOT context, you'll get
rid of the /context part (but then, this might make life difficult
for any other servlet applications on the same server, as the context
names for them should not clash with resource names within your
application).

The :port element is redundant, if your server is running on port 80.
However, depending on your operating system, running on port <1024
might require special privileges on the account running your Tomcat.

So, with these you're down to
http://hostname/yourservlet

but you may have to run the servlet engine with a privileged user,
and you're risking path conflict with possible other servlet
applications running in the same engine.
 

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

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top