aspose file tools*
The moose likes Servlets and the fly likes How to specify a query string in servlet mapping? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to specify a query string in servlet mapping?" Watch "How to specify a query string in servlet mapping?" New topic
Author

How to specify a query string in servlet mapping?

Al Koch
Ranch Hand

Joined: Aug 24, 2011
Posts: 46

Hello,

I am running Tomcat 5.5.4 and have a servlet running with no problems. However, I'd like to set up a mapping to only launch the servlet when a URL containing a particular query string is submitted.

Right now in web.xml I have:
<servlet-mapping>
<servlet-name>MyServer</servlet-name>
<url-pattern>/go/*</url-pattern>
</servlet-mapping>

If a browser submits http://localhost/MyServer/go?P=123 the servlet is launched and all is well. However, I'd like to only launch that servlet if the URL is exactly as just shown. Unfortunately, right now if the URL is http://localhost/MyServer/go?P=AnyDarnThing the servlet still launches. I have tried setting up the following:
<url-pattern>/go?P=123</url-pattern>
but this results in The requested resource (/MyServer/go) is not available.

I've tried numerous variations (quoting the string, ...) on the above URL pattern but I always get the above error. I notice that if I (for debugging purposes) drop the "?" as in
<url-pattern>/goP=123</url-pattern>
I no longer get the error message and the server launches (but, of course, it doesn't respond to the "query string" because it's not properly formed.) This suggest to me that the "?" is causing a problem in the mapping. I've tried replacing it with its URL special character equivalent as follows:
<url-pattern>/go%3FP=123</url-pattern>
but this gives the same result just described above when I tried dropping the "?" altogether.

I realize I can let the servlet get launched when any query string is submitted and then "ignore" the request for all but the one I care about but there is a reason I'd prefer to not have the servlet launched to begin with. So, my question is, how can I configure the servlet so that it is only launched when a specific query string is included?

Thank you.
sarada bokka
Ranch Hand

Joined: Jan 13, 2010
Posts: 80
write a filter and check whether it contains the query string.

As per I knew url pattern it to restrict a path not to restrict query string.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

You cannot restrict it by query string.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Al Koch
Ranch Hand

Joined: Aug 24, 2011
Posts: 46

Thanks Sarada and Bear for your replies. Looks like I can't do this! Sarada, you suggested "writing a filter". I know nothing about this - can you suggest a resource where I can learn how to do this?

Thank you.
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

use the req.getQueryString() method to intercept your url inside the filter.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

The Servlet Specification contains all the information you need to learn to write a filter. There are also web tutorials.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to specify a query string in servlet mapping?