简体   繁体   English

如何在Tomcat的<servlet-mapping> <url-pattern>中指定查询字符串?

[英]How do I specify a query string in Tomcat's <servlet-mapping> <url-pattern>?

I am running Tomcat 5.5.4 and have a servlet running with no problems. 我正在运行Tomcat 5.5.4并且运行没有问题的servlet。 However, I'd like to set up a mapping to only launch the servlet when a URL containing a particular query string is submitted. 但是,我想设置一个映射,以便在提交包含特定查询字符串的URL时仅启动servlet。

Right now in web.xml I have: <servlet-mapping> <servlet-name>MyServer</servlet-name> <url-pattern>/go/*</url-pattern> </servlet-mapping> 现在在web.xml中我有: <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. 如果浏览器提交http:// localhost / MyServer / go?P = 123 ,则启动servlet并且一切正常。 However, I'd like to only launch that servlet if the URL is exactly as just shown. 但是,如果URL完全如图所示,我只想启动该servlet。 Unfortunately, right now if the URL is http://localhost/MyServer/go?P=AnyDarnThing the servlet still launches. 不幸的是,现在如果URL是http:// localhost / MyServer / go?P = AnyDarnThing ,servlet仍然会启动。 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. 我已尝试设置以下内容: <url-pattern>/go?P=123</url-pattern>但这会导致请求的资源(/ MyServer / go)不可用。

I've tried numerous variations (quoting the string, ...) on the above URL pattern but I always get the above error. 我在上面的URL模式上尝试了很多变化(引用字符串,...),但我总是得到上面的错误。 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 "?" 如在<url-pattern>/goP=123</url-pattern>我不再收到错误消息并且服务器启动(但是,当然,它不响应“查询字符串”,因为它没有正确形成。)这告诉我“?” 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 "?" 我已经尝试用它的URL特殊字符替换它,如下所示: <url-pattern>/go%3FP=123</url-pattern>但是当我尝试删除“?”时,这给出了上面描述的相同结果。 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. 我意识到,当提交任何查询字符串时,我可以让servlet启动,然后“忽略”除了我关心的所有请求之外的所有请求,但有一个原因我宁愿不启动servlet。 So, my question is, how can I configure the servlet so that it is only launched when a specific query string is included? 所以,我的问题是,如何配置servlet以便仅在包含特定查询字符串时启动它?

Thank you. 谢谢。

You can't do that. 你不能这样做。 The url-pattern is pretty limited. url-pattern非常有限。

If you want to have distinct actions taken based on a GET parameter, you can do that manually. 如果您希望根据GET参数执行不同的操作,则可以手动执行此操作。 In the doGet() method of the servlet have a simple if -clause and invoke different methods depending on the query string / get param. 在servlet的doGet()方法中有一个简单的if -clause,并根据查询字符串/ get param调用不同的方法。

You can't do that using URL patterns. 你不能使用URL模式来做到这一点。

You can achive this using filters. 您可以使用过滤器来实现此目的。 Implement a filter which will forward to the Servlet only if the query params exists. 实现一个过滤器,只有在查询参数存在时才会转发到Servlet。

Here is the how the filter will look like: 以下是过滤器的外观:

public class ServletAcessFilter implements Filter
{
  public void init(FilterConfig filterConfig) throws ServletException
  {

  }

  public void doFilter(ServletRequest request, ServletResponse response,
  FilterChain filterChain) throws IOException, ServletException
  {
    //validate the request, check if the request can be forwarded to servlet.
    if(request.getParameter("P").equalsIgnoreCase("123")){
        filterChain.doFilter(request, response);
    } else {
        //write what you want to do if the request has no access
        //below code will write 404 not found, you can do based on your requirement
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setStatus(404);
    }
  }

  public void destroy()
  {

  }
}

Define the filter in the web.xml like this: web.xml定义过滤器,如下所示:

<filter>
    <filter-name>ServletAccessFilter</filter-name>
    <filter-class>com.ServletAcessFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ServletAccessFilter</filter-name>
    <url-pattern>/go/*</url-pattern>
</filter-mapping>

To add to Bozho response, you may also try to move to Clean URLs 要添加到Bozho响应,您还可以尝试转到Clean URL

This will greatly increase your options in terms of URL pattern matching, and, in particular, may significantly ease configuration of a fronting reverse proxy if you ever need one. 这将极大地增加您在URL模式匹配方面的选择,特别是,如果您需要,可以显着简化前端反向代理的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM