简体   繁体   English

Servlet:获取查询字符串,但只返回空

[英]Servlet: get query string but just return empty

I'm using mediator design pattern for my web project. 我在我的Web项目中使用中介器设计模式。 There is a front controller to take care all requests, and base on each request to find suitable servlet. 有一个前端控制器来处理所有请求,并根据每个请求找到合适的servlet。 In web.xml, I try this: 在web.xml中,我尝试这样做:

<servlet-mapping>
         <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>

And then a request like localhost:8080/project_name/a/b/c/d.jsp I want to get the string a/b/c/d.jsp but when I use: request.getServletPath() I get empty string. 然后一个类似localhost:8080 / project_name / a / b / c / d.jsp的请求,我想获得字符串a/b/c/d.jsp但是当我使用时: request.getServletPath()我得到了空字符串。

Thanks :) 谢谢 :)

You should use getPathInfo() method instead 您应该改用getPathInfo()方法

getServletPath() method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern. 如果用于处理此请求的servlet使用“ / *”模式匹配,则getServletPath()方法将返回一个空字符串(“”)。

getPathInfo() method returns path information follows the servlet path but precedes the query string and will start with a "/" character. getPathInfo()方法返回路径信息,该信息位于servlet路径之后,但位于查询字符串之前,并将以“ /”字符开头。

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

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