• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Specifying Servlet URL

 
Rajeev Asthana
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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.
 
S Subramonyan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic