• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Specifying Servlet URL

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can I specify any URL for accessing a Servlet in a HTML/ JSP whether that servlet class
is present in that URL or not?

I'll then specify it in <Servlet-mapping> tags and specify URL pattern with this URL.

What is the general practice?

Thanks in advance.

Rajeev.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can, for eg:

You have a webapp named test. You have a servlet class named MyServlet.class

You can specify any name for it in the dd element <servlet-name>, and you can map it to any url pattern you want.

eg: in web.xml,

<web-app>
......
..
<servlet>
<servlet-name>OhMy</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
..
..

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

<servlet-mapping>
<servlet-name>OhMy</servlet-name>
<url-pattern>/ohh.*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>OhMy</servlet-name>
<url-pattern>/foo.baz</url-pattern>
</servlet-mapping>

..
</web-app>

Hope this helps!
 
Rajeev Asthana
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by S Subramonyan:
Yes you can, for eg:

You have a webapp named test. You have a servlet class named MyServlet.class

You can specify any name for it in the dd element <servlet-name>, and you can map it to any url pattern you want.

eg: in web.xml,

<web-app>
......
..
<servlet>
<servlet-name>OhMy</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
..
..

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

<servlet-mapping>
<servlet-name>OhMy</servlet-name>
<url-pattern>/ohh.*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>OhMy</servlet-name>
<url-pattern>/foo.baz</url-pattern>
</servlet-mapping>

..
</web-app>

Hope this helps!



If this is possible, how Web Container knows where to locate the servlet when invoked from a HTML/JSP?
 
author
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajeev Asthana:
If this is possible, how Web Container knows where to locate the servlet when invoked from a HTML/JSP?



First of all, it is best to say "How does a web browser invoke a servlet?" Because it is not really the HTML (or HTML generated from a JSP) that does the invoking.

Second, "invoke a servlet" really means "send an HTTP request that invokes a servlet." So, what goes into the HTTP request that tells the web container which servlet to invoke? That would be the URL. So, if a web page (the HTML) has the following hyperlink:

Then when the user clicks on that link the web browser sends an HTTP GET request for the foo.baz resource. The web container will use the servlet mappings defined in the deployment descriptor (as shown in S. Subramonyan's reply) to identify the 'OhMy' servlet. This servlet is an instance of the MyServlet class, which was defined in the <servlet> definition in the DD.

Does that help?

-Bryan
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic