问题描述
我正在使用jetty 9.0.3。
I'm using jetty 9.0.3.
如何将URL(如www.myweb.com/{servlet}/{parameter})映射到给定的servlet和参数?
How to map an URL such as www.myweb.com/{servlet}/{parameter} to the given servlet and parameter?
例如,URL/ client / 12312将路由到clientServlet及其 doGet
方法将收到12312作为参数。
For example, the URL '/client/12312' will route to clientServlet and its doGet
method will receive 12312 as a parameter.
推荐答案
您将有两个部分来担心。
You'll have 2 parts to worry about.
- 您的
WEB-INF / web.xml
- 中的pathSpec 在您的servlet中。
- The pathSpec in your
WEB-INF/web.xml
- The HttpServletRequest.getPathInfo() in your servlet.
pathSpec
code> WEB-INF / web.xml 你必须声明你的Servlet和你的url模式(也称为pathSpec)。
The pathSpec
In your WEB-INF/web.xml
you have to declare your Servlet and your url-patterns (also known as the pathSpec).
示例:
<?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>
这将设置实现为类的servlet com.mycompany.ClientServlet
client code clientCvlet
然后指定用于传入请求URL的 / client / *
的url模式
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.
url-pattern结尾处的额外
/ *
允许以 / client /
被接受,这对于pathInfo部分很重要。
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.
接下来我们进入我们的Servlet实现。
Next we get into our Servlet implementation.
在你的值,它将接收url模式上
/ client
之后的请求URL部分。
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.
示例:
Request URL Path Info
---------------- ------------
/client/ /
/client/hi /hi
/client/world/ /world/
/client/a/b/c /a/b/c
您可以根据路径信息
这篇关于Jetty'{servlet} / {parameter}'url路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!