The Artima Developer Community
Sponsored Link

Java Answers Forum
How do I get <url-pattern> of a servlet ?

1 reply on 1 page. Most recent reply: Apr 23, 2002 5:09 PM by Jay Kandy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
smallufo

Posts: 1
Nickname: smallufo
Registered: Apr, 2002

How do I get <url-pattern> of a servlet ? Posted: Apr 22, 2002 12:34 AM
Reply to this message Reply
Advertisement
How do I dynamically put a link to another servlet under the same WebApp

In a servlet1 , I want to write a link to another servlet2
servlet1 and servlet2 are under the same WebApp
I don't want to hardcode the link in the URL...

The problem I encounter is that it seems ServletSpec didn't mention
how to get one servlet's <url-pattern> value !!

for example , I set a web-app id to '/' in resin.conf by this :


<host id='k3.smallufo.com'>
<error-log id='log/error.log' />
<web-app id='/' app-dir='/home/k3/www' />
</host>


And in /home/k3/www/WEB-INF/web.xml , I set the following <URL-PATTERN>


<web-app>
<!-- SKIP -->
<servlet-mapping url-pattern='/servlet/*' servlet-name='invoker' />
</web-app>


testServlet's full qualified name is : com.smallufo.k3.webapp.testServlet
Another testServlet2's full qualified name is : com.smallufo.k3.webapp.testServlet2

I want to write a link in testServlet to testServlet2 , by not hardcoding the URL in servlet code
therefore , I write like this :


String URL = req.getScheme()+"://"+
req.getServerName() + ":" +
req.getServerPort() +
req.getContextPath()+
"/com.smallufo.k3.webapp.testServlet2";


but it comes the problem...It cannot work , because it combines like this :
'http://k3.smallufo.com:80/com.smallufo.k3.webapp.testServlet2'
In Servlet spec , there is no way to get the URL-PATTERN = '/servlet/' !!!

The only one way I can think of is by using HttpServletRequest.getServletPath()
to get '/servlet/com.smallufo.k3.webapp.testServlet' , and process the string to get '/servlet/'
but it is too inconvenient !!!

Is there any better solution ?


Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: How do I get <url-pattern> of a servlet ? Posted: Apr 23, 2002 5:09 PM
Reply to this message Reply
Correct if I am wrong but isnt it a servlet container's job to invoke a servlet based on the <url-pattern> and not the other way around? The method getServletPath gives the path of the servlet that is serving the request. So parsing for /servlet/ in the servlet path wouldnt work unless it was a conincidence that the two servlets have the same servlet path. Consider if your web.xml read this way:

<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/servlet/Servlet1</url-pattern>
</servlet-mapping&g t;

<servlet-mapping>
<servlet-name>Servlet2</servlet-name>
<url-pattern>/hello/world/Servlet2</url-pattern>
</servlet-mappi ng>

Flat View: This topic has 1 reply on 1 page
Topic: ImageCopy Previous Topic   Next Topic Topic: setting the cursor to the next aTextField

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use