简体   繁体   English

Jetty'{servlet} / {parameter}'网址路由

[英]Jetty '{servlet}/{parameter}' url routing

I'm using jetty 9.0.3. 我正在使用jetty 9.0.3。

How to map an URL such as www.myweb.com/{servlet}/{parameter} to the given servlet and parameter? 如何将URL(例如www.myweb.com/{servlet}/{parameter})映射到给定的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作为参数。

You'll have 2 parts to worry about. 你有2个部分需要担心。

  1. The pathSpec in your WEB-INF/web.xml WEB-INF/web.xml的pathSpec
  2. The HttpServletRequest.getPathInfo() in your servlet. servlet中的HttpServletRequest.getPathInfo()

The pathSpec 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模式(也称为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. 这将在名称clientServlet上设置为com.mycompany.ClientServlet类的servlet,然后为传入的请求URL指定/client/*的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末尾的extra /*允许接受以/client/开头的任何传入模式,这对于pathInfo部分很重要。

The pathInfo 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()值,该值将接收url-pattern上/client之后的请求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 此时,您可以根据路径信息对信息执行任何逻辑操作

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 vs @PathParam

@Path("v1/services/{entity}")
@GET
public class RequestHandler(@PathParam("entity")String entity, @PathParam("id")String id){
   @path({id})
   public Entity handle(){

   }
}

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

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