GVKun编程网logo

分派器servlet弹簧和URL模式

16

对于分派器servlet弹簧和URL模式感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于JavaJSP/Servlet:控制器Servlet抛出著名的堆栈溢出、JavaServlet–将s

对于分派器servlet弹簧和URL模式感兴趣的读者,本文将提供您所需要的所有信息,并且为您提供关于Java JSP / Servlet:控制器Servlet抛出著名的堆栈溢出、Java Servlet – 将servlet映射到每个URL但字符串、java – Dispatcher servlet spring和url模式、java – Servlet:如何获取自己的URL模式?的宝贵知识。

本文目录一览:

分派器servlet弹簧和URL模式

分派器servlet弹簧和URL模式

我是Spring框架的新手,今天我在web.xml文件中遇到了调度程序servlet配置,然后想到了一个有关url模式的问题,例如语法/。因此,如果我按如下所示在tomcat服务器中部署Web应用程序,“
/”符号实际上适用什么:host:port /或host:port / myWeb /

答案1

小编典典

该模式/将使您的servlet成为该应用程序的默认servlet,这意味着它将选择没有另一个完全匹配的所有模式。

URL模式映射:

  • 以/字符开头并以/*后缀结尾的字符串用于路径映射。
  • *.前缀开头的字符串用作扩展名映射。
  • 仅包含/字符的字符串表示应用程序的 默认
    servlet。在这种情况下,Servlet路径是请求URI减去上下文路径,路径信息是null
  • 所有其他字符串仅用于完全匹配。

路径映射规则:

  1. 容器将尝试查找请求路径与Servlet路径的精确匹配。成功匹配将选择servlet。
    2.
    容器将递归地尝试匹配最长的路径前缀。这是通过使用/字符作为路径分隔符,一次将路径树下移到一个目录来完成的。最长的匹配确定所选的servlet。
    3.
    如果URL路径中的最后一段包含扩展名(例如.jsp),则servlet容器将尝试匹配处理该扩展名请求的servlet。扩展名定义为最后一个.字符之后的最后一段的一部分。
  2. 如果前三个规则均未导致servlet匹配,则容器将尝试提供适合于所请求资源的内容。如果为应用程序定义了 默认的
    servlet,则将使用它。

Java JSP / Servlet:控制器Servlet抛出著名的堆栈溢出

Java JSP / Servlet:控制器Servlet抛出著名的堆栈溢出

我已经阅读了几份文档,但我听不懂:我知道我做错了什么,但我不明白。我有一个完全动态生成的网站:几乎没有静态内容。

因此,为了理解JSP / Servlet,我编写了自己的“前端控制器”来拦截每个查询,如下所示:

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

基本上,我需要任何用户请求,例如:

  • example.org
  • example.org/bar
  • example.org/foo.html

所有都经过我编写的默认servlet。

然后,该Servlet检查URI并查找必须将请求分派到哪个 .jsp ,然后在正确设置所有属性后执行以下操作:

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
dispatcher.forward(req,resp);

当我使用url-pattern(在 web.xml中
)时,例如*.html,一切正常。但是,当我将其更改为/*(以真正拦截所有内容)时,我进入了一个无限循环,最后以…
StackOverflow结尾:

分派请求时,URI “ … / WEB-INF / jsp / index.jsp” 本身是否与我设置的 web.xml 过滤器/ * 相匹配?

编辑 显然没有,因为这是对 index.jsp 的精确映射,因此它绕过了web.xml url模式。所以我仍然不知道如何进入无尽的循环。

如果我想使用/ * url-pattern拦截所有内容但又能够调度/转发/,该怎么办?

我在这里不是在问规范/ Javadocs:我真的对更大的图景感到困惑,我需要对可能发生的事情进行一些解释。

我不应该拦截所有东西吗?

如果我可以拦截所有内容,那么关于转发/调度我应该注意些什么?

Java Servlet – 将servlet映射到每个URL但字符串

Java Servlet – 将servlet映射到每个URL但字符串

我有一个servlet配置为处理所有URL(*):
<servlet>
    <servlet-name>MyServ</servlet-name>
    <servlet-class>MyServlet</servlet-class>
</servlet>

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

我需要以/ static /开头的URLS,它应该从静态WEB-INF提供给它们.也就是说,MyServ应该服务于/ static.

我怎样才能做到这一点?

更新:要澄清,我想要的是:

/ * / – 转到MyServ
/static/dir/file.css – Jetty从/ dir /中提供静态file.css.

我不知道web.xml要做什么,或者把静态文件放在哪里.

我尝试添加:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

但是,当我去/静态/ URL,我只是得到:

HTTP ERROR 404

Problem accessing /static/dir/file.css. Reason: 
    Not Found

Powered by Jetty://

我不知道我的web.xml是否错误,或者我只是将文件放在错误的地方(我已经在src / main / webapp和src / main / webapp / lib / meta-inf /资源/)

码头

我正在使用码头.我想避免任何其他层,如Nginx,Apache等.

要赢得赏金,请确保您回答Jetty的作品.

解决方法

你最好的赌注可能是在*的规则之前有一个静态规则.

Rule for URL path mapping:

It is used in the following order. First successful match is used with no further attempts.

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time,using the ’/’ character as a path separator. The longest match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp),the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.
  4. If neither of the prevIoUs three rules result in a servlet match,the container will attempt to serve content appropriate for the resource requested. If a “default” servlet is defined for the application,it will be used.

所以它将匹配/ static /的规则,并停止在那里.

java – Dispatcher servlet spring和url模式

java – Dispatcher servlet spring和url模式

我是 spring框架的新手今天我遇到了web.xml文件中的调度程序servlet配置,我想出了一个关于url模式的问题,比如这个语法/.那么,如果我在tomcat服务器中部署web应用程序,实际上“/”符号适用的是:host:port /或host:port / myWeb /

解决方法

模式/将使您的servlet成为应用程序的默认servlet,这意味着它将获取没有其他完全匹配的每个模式.

URL模式映射:

  • A string beginning with a / character and ending with a /* suffix is used for path mapping.
  • A string beginning with a *. prefix is used as an extension mapping.
  • A string containing only the / character indicates the default servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  • All other strings are used for exact matches only.

路径映射规则:

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time,using the / character as a path separator. The longest match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp),the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last . character.
  4. If neither of the prevIoUs three rules result in a servlet match,the container will attempt to serve content appropriate for the resource requested. If a default servlet is defined for the application,it will be used.

java – Servlet:如何获取自己的URL模式?

java – Servlet:如何获取自己的URL模式?

servlet或过滤器可以查找自己的URL模式吗?

意思是,如果我将一些servlet或过滤器绑定到/ first / *和/ second / *并且请求进来,我可以找出这两个模式中的哪一个触发了它吗?

即使servlet只绑定到一个模式,有没有办法从servlet内部查找(而不是硬编码一个值)?

解决方法

HttpServletRequest类上的这个方法可以帮到你.您将在HTTP请求调用的任何Servlet方法上获得HttpServletRequest的实例.

getServletPath

java.lang.String getServletPath() Returns the part of this request’s
URL that calls the servlet. This path starts with a “/” character and
includes either the servlet name or a path to the servlet,but does
not include any extra path information or a query string. Same as the
value of the CGI variable SCRIPT_NAME.

看看这个:

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath()

关于分派器servlet弹簧和URL模式的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Java JSP / Servlet:控制器Servlet抛出著名的堆栈溢出、Java Servlet – 将servlet映射到每个URL但字符串、java – Dispatcher servlet spring和url模式、java – Servlet:如何获取自己的URL模式?等相关知识的信息别忘了在本站进行查找喔。

本文标签: