Servlet URL 映射
我有两个 servlet(MainServlet 和 LoginServlet),MainServlet 处理所有请求并将其映射到 /*
。 LoginServlet 处理请求并将其映射到/login。我有一个 html 文件 /html/login.html。现在我想在点击 http://localhost:8080/app/login 时显示这个html文件。
在 LoginServlet doGet
方法中,我正在执行 httpServletRequest.getRequestDispatcher("login/login.html").include(httpServletRequest, httpServletResponse);
但此重定向请求到 MainServlet
。我无法将 MainSerlvet
的 url 映射从 /*
更改为其他内容。
我有什么想法可以实现上述目标吗? PS:如果问题不清楚,请告诉我。
I have two servlets (MainServlet and LoginServlet) and MainServlet process all request and its mapped to /*
. LoginServlet process request and it is mapped to /login. I have one html file /html/login.html. Now I want to show this html file when I hit http://localhost:8080/app/login .
In LoginServlet doGet
method I am doing httpServletRequest.getRequestDispatcher("login/login.html").include(httpServletRequest, httpServletResponse);
but this redirect request to MainServlet
. I can't change url mapping of MainSerlvet
from /*
to something else.
Any Idea how I can achieve above?
PS: If question is not clear please tell me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您已将
MainServlet
映射到全局 URL 模式/*
上,这对于 servlet 来说是一个非常糟糕的做法(这也涵盖了 CSS/JS/images/etc 等静态资源! )。这还将拦截所有转发和包含的请求。您需要将MainServlet
映射到更具体的 URL 模式,例如/main/*
、/app/*
或类似的内容并创建映射到/*
的Filter
并将所有非/login
请求转发到MainServlet
。顺便说一句,使用 RequestDispatcher#include() 来显示视图也不完全正确。您应该使用
RequestDispatcher#forward()
来代替。You've mapped the
MainServlet
on a global URL pattern/*
which is a pretty poor practice for servlets (this also covers static resources like CSS/JS/images/etc!). This will also intercept on all forwarded and included requests. You need to map theMainServlet
on a more specific URL pattern, e.g./main/*
,/app/*
or something like that and create aFilter
which is mapped on/*
and forwards all non-/login
requests to theMainServlet
.By the way, using
RequestDispatcher#include()
to display the view is not entirely correct as well. You should be usingRequestDispatcher#forward()
for this instead.