java Jetty '{servlet}/{parameter}' url 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16965162/
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
Jetty '{servlet}/{parameter}' url routing
提问by mbmihura
I'm using jetty 9.0.3.
我正在使用码头 9.0.3。
How to map an URL such as www.myweb.com/{servlet}/{parameter} to the given servlet and parameter?
如何将诸如 www.myweb.com/{servlet}/{parameter} 的 URL 映射到给定的 servlet 和参数?
For example, the URL '/client/12312' will route to clientServlet and its doGet
method will receive 12312 as a parameter.
例如,URL '/client/12312' 将路由到 clientServlet 并且其doGet
方法将接收 12312 作为参数。
回答by Joakim Erdfelt
You'll have 2 parts to worry about.
您将有 2 个部分需要担心。
- The pathSpec in your
WEB-INF/web.xml
- The HttpServletRequest.getPathInfo()in your servlet.
- 你的 pathSpec
WEB-INF/web.xml
- servlet 中的HttpServletRequest.getPathInfo()。
The pathSpec
路径规范
In your WEB-INF/web.xml
you have to declare your Servlet and your url-patterns (also known as the pathSpec).
在您WEB-INF/web.xml
必须声明您的 Servlet 和您的 url-patterns(也称为 pathSpec)。
Example:
例子:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false"
version="3.0">
<display-name>Example WebApp</display-name>
<servlet>
<servlet-name>clientServlet</servlet-name>
<servlet-class>com.mycompany.ClientServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>clientServlet</servlet-name>
<url-pattern>/client/*</url-pattern>
</servlet-mapping>
</web-app>
This sets up the servlet implemented as class com.mycompany.ClientServlet
on the name clientServlet
then specifies the url-pattern of /client/*
for incoming request URLs.
这将设置作为com.mycompany.ClientServlet
名称上的类实现的 servlet,clientServlet
然后指定/client/*
传入请求 URL的 url-pattern 。
The extra /*
at the end of the url-pattern allows any incoming pattern that starts with /client/
to be accepted, this is important for the pathInfo portion.
/*
url-pattern 末尾的额外内容允许/client/
接受任何以 开头的传入模式,这对于 pathInfo 部分很重要。
The pathInfo
路径信息
Next we get into our Servlet implementation.
接下来我们进入我们的 Servlet 实现。
In your doGet(HttpServletRequest req, HttpServletResponse resp)implementation on ClientServlet you should access the req.getPathInfo()value, which will receive the portion of the request URL that is after the /client
on your url-pattern.
在ClientServlet 上的doGet(HttpServletRequest req, HttpServletResponse resp)实现中,您应该访问req.getPathInfo()值,该值将接收/client
在您的 url-pattern之后的请求 URL 部分。
Example:
例子:
Request URL Path Info
---------------- ------------
/client/ /
/client/hi /hi
/client/world/ /world/
/client/a/b/c /a/b/c
At this point you do whatever logic you want to against the information from the Path Info
此时,您可以根据路径信息中的信息执行任何您想要的逻辑
回答by Alex Stanovsky
You can use Jersey
and register the following class in the ResourceConfig
packages, which is handling ../worker/1234
url patterns.
您可以Jersey
在ResourceConfig
包中使用和注册以下类,该类用于处理../worker/1234
url 模式。
read more: When to use @QueryParam vs @PathParam
阅读更多:何时使用 @QueryParam 与 @PathParam
@Path("v1/services/{entity}")
@GET
public class RequestHandler(@PathParam("entity")String entity, @PathParam("id")String id){
@path({id})
public Entity handle(){
}
}