Servlet URL 映射

发布于 12-21 03:18 字数 589 浏览 1 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱2024-12-28 03:18:12

您已将 MainServlet 映射到全局 URL 模式 /* 上,这对于 servlet 来说是一个非常糟糕的做法(这也涵盖了 CSS/JS/images/etc 等静态资源! )。这还将拦截所有转发和包含的请求。您需要将 MainServlet 映射到更具体的 URL 模式,例如 /main/*/app/* 或类似的内容并创建映射到 /*Filter 并将所有非 /login 请求转发到 MainServlet

String uri = ((HttpServletRequest) request).getRequestURI();

if (uri.startsWith("/login/")) {
    // Just continue to login servlet.
    chain.doFilter(request, response);
} else {
    // Forward to main servlet.
    request.getRequestDispatcher("/main" + uri).forward(request, response);
}

顺便说一句,使用 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 the MainServlet on a more specific URL pattern, e.g. /main/*, /app/* or something like that and create a Filter which is mapped on /* and forwards all non-/login requests to the MainServlet.

String uri = ((HttpServletRequest) request).getRequestURI();

if (uri.startsWith("/login/")) {
    // Just continue to login servlet.
    chain.doFilter(request, response);
} else {
    // Forward to main servlet.
    request.getRequestDispatcher("/main" + uri).forward(request, response);
}

By the way, using RequestDispatcher#include() to display the view is not entirely correct as well. You should be using RequestDispatcher#forward() for this instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文