Java Servlet @WebServlet urlPatterns
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16578694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet @WebServlet urlPatterns
提问by kakacii
This is a quick question but I couldn't find a quick answer. Now I have a servlet BaseServlet, when user request any of the url below:
这是一个快速的问题,但我找不到快速的答案。现在我有一个 servlet BaseServlet,当用户请求以下任何 url 时:
host
host/
host/BaseServlet
It should always refer to the same servlet and redirect to the homepage.
它应该总是引用同一个 servlet 并重定向到主页。
When I set
当我设置
@WebServlet({"/BaseServlet", ""})
Only
仅有的
host/
host/BaseServlet
works
作品
If I set
如果我设置
@WebServlet({"/BaseServlet", "", "/"})
The BaseServlet will be requested constantly in loop ...
BaseServlet 将在循环中不断被请求......
Why?
为什么?
Edit: BaseServlet does a forward to the index.html hid in WEB-INF folder and that's it.
编辑:BaseServlet 转发到隐藏在 WEB-INF 文件夹中的 index.html,就是这样。
getServletContext().getRequestDispatcher("/WEB-INF/index.html").forward(request,response);
The servlet spec says "A string containing only the / character indicates the "default" servlet of the application." So I want the BaseServlet to be my default. Why it doesn't work?
servlet 规范说“仅包含 / 字符的字符串表示应用程序的“默认”servlet。所以我希望 BaseServlet 成为我的默认值。为什么它不起作用?
回答by Snehal Patel
Edit:
编辑:
If you want to pre-process then you can use Filter with url-pattern "/*" and dispatcher set to REQUEST that way it will ignore forward.
如果您想进行预处理,那么您可以使用带有 url-pattern "/*" 的过滤器,并将调度程序设置为 REQUEST,这样它将忽略转发。
Last value "/" means all request.
最后一个值“/”表示所有请求。
Checkout discussion at: http://www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern
结帐讨论:http: //www.coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern
And inside Servlet another forward request for index.html is generated which is also intercepted by servlet.
并且在 Servlet 内部生成另一个 index.html 的转发请求,该请求也被 servlet 拦截。
If you try @WebServlet({"/BaseServlet", "/"}) which is same as @WebServlet({"/BaseServlet", "", "/"}) will result in same error.
如果您尝试与 @WebServlet({"/BaseServlet", "", "/"}) 相同的 @WebServlet({"/BaseServlet", "/"}) 将导致相同的错误。
You can check this by typing following output statement in servlet:
您可以通过在 servlet 中键入以下输出语句来检查这一点:
System.out.println(req.getRequestURL());
回答by Glen Best
As you state in your Q, if you want the following:
host/ host/BaseServlet
Use
@WebServlet({"/BaseServlet", ""})
If you want the following:
host
Add this to your welcome file (you can't specifiy welcome files using annotations)
<welcome-file-list> <welcome-file>/BaseServlet</welcome-file> </welcome-file-list>
The servlet spec says "A string containing only the '/' character indicates the "default" servlet of the application."
But it says straight afterwards
In this case the servlet path is the request URI minus the context path and the path info is null.
In other words, if your URL is
host
then the servlet path will be
"" (empty string)
so you will need a welcome file list (but index.htm[l] and index.jsp in the webapp directory, not WEB-INF, are included implicitly as a starting welcome file list)
正如您在 Q 中所述,如果您想要以下内容:
host/ host/BaseServlet
用
@WebServlet({"/BaseServlet", ""})
如果您想要以下内容:
host
将此添加到您的欢迎文件中(您不能使用注释指定欢迎文件)
<welcome-file-list> <welcome-file>/BaseServlet</welcome-file> </welcome-file-list>
servlet 规范说“仅包含 '/' 字符的字符串表示应用程序的“默认”servlet。
但它之后直接说
在这种情况下,servlet 路径是请求 URI 减去上下文路径,路径信息为空。
换句话说,如果您的网址是
host
那么servlet路径将是
"" (empty string)
因此您将需要一个欢迎文件列表(但 webapp 目录中的 index.htm[l] 和 index.jsp,而不是 WEB-INF,作为起始欢迎文件列表隐式包含在内)