简体   繁体   English

为什么每个URL都“重定向”到欢迎文件URL?

[英]Why is every url “redirecting” to the welcome-file url?

Servlets 小程序

@WebServlet("/")
public class AServlet extends HttpServlet {

-- loads an .jsp and displays "AServlet" -加载.jsp并显示“ AServlet”

@WebServlet("/b")
public class BServlet extends HttpServlet {

-- returns an simple JSON -返回一个简单的JSON

index.html index.html

-- displays "Not a JSP" -显示“不是JSP”

web.xml web.xml

...
<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>
...

Every url that doesn't match an servlet executes the same action as the A servlet. 每个与servlet不匹配的url都执行与A servlet相同的操作。

Eg: 例如:

  • ...localhost.../AppName/ -> displays AServlet - ok ... localhost ... / AppName /->显示AServlet- 确定
  • ...localhost.../AppName/ loremipsum -> displays AServlet - WHY DOESN'T IT RETURN A 404 NOT FOUND? ... localhost ... / AppName / loremipsum- >显示AServlet- 为什么不返回404错误?
  • ...localhost.../AppName/ b -> returns JSON - ok ... localhost ... / AppName / b- >返回JSON- 确定
  • ...localhost.../AppName/ index.html -> even if the file exists, still displays "AServlet" ... localhost ... / AppName / index.html- >即使文件存在,仍显示“ AServlet”

将Servlet映射到“ /”使其成为默认Servlet,这意味着它将处理所有与任何其他映射都不匹配的请求。

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

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