如何将JSP正确放入WEB-INF中?

发布于 2024-12-23 06:36:27 字数 870 浏览 2 评论 0原文

我正在使用 MVC,并且希望将 JSP 页面放在 WEB-INF 中以避免直接访问它。我在 Web 内容的 jsp 文件夹中有一个 index.jsp 页面和其他页面,并且它可以工作。它看起来像这样:

-Web Content
-index.jsp
-jsp
--main_read.jsp
--...

顺便说一句,index.jsp 是我的登录页面,无论用户是否登录,在我使用的控制器中

RequestDispatcher dispatcher =
        getServletContext().getRequestDispatcher("jsp/main_read.jsp");
dispatcher.forward(request, response);

我工作得很好,但是当我尝试将 JSP 放入 WEB-INF 时,它失败了:

-Web Content
-index.jsp
-WEB-INF
--jsp
---jsp
----main_read.jsp
----...

并给出了像这样的错误

HTTP Status 404 - /Libruary/jsp/main_read.jsp

type Status report

message /Libruary/jsp/main_read.jsp

description The requested resource (/Libruary/jsp/main_read.jsp) is not available.
Apache Tomcat/6.0.26

可能问题出在页面路径中,我在 dispatcher.forward 中编写,但无论如何,请帮助我。

I'm using MVC and want to put my JSP pages in WEB-INF to avoid direct access to it. I have an index.jsp page and other pages in jsp folder in Web Content and it works. It looks like this:

-Web Content
-index.jsp
-jsp
--main_read.jsp
--...

By the way, index.jsp is my login page and whether user is logged, in controller I use

RequestDispatcher dispatcher =
        getServletContext().getRequestDispatcher("jsp/main_read.jsp");
dispatcher.forward(request, response);

I works perfect, but when I'm trying to put my JSP in WEB-INF it fails:

-Web Content
-index.jsp
-WEB-INF
--jsp
---jsp
----main_read.jsp
----...

And gives an error like this

HTTP Status 404 - /Libruary/jsp/main_read.jsp

type Status report

message /Libruary/jsp/main_read.jsp

description The requested resource (/Libruary/jsp/main_read.jsp) is not available.
Apache Tomcat/6.0.26

Probably the problem is in the page path, I write in dispatcher.forward, but anyway, help me please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜巴黎 2024-12-30 06:36:27

您似乎传递了路径/jsp/main_read.jsp,并且JSP位于/WEB-INF/jsp/jsp/main_read.jsp中。显然,路径不匹配。将正确的路径传递给 getRequestDispatcher()/WEB-INF/jsp/jsp/main_read.jsp

javadoc 说:

路径名必须以 / 开头,并被解释为相对于
当前上下文根

You seem to pass the path /jsp/main_read.jsp, and the JSP is in /WEB-INF/jsp/jsp/main_read.jsp. Obviously, the paths don't match. Pass the correct path to getRequestDispatcher(): /WEB-INF/jsp/jsp/main_read.jsp.

The javadoc says:

The pathname must begin with a / and is interpreted as relative to the
current context root

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