如何在Servlet中插入JSP功能?

发布于 2024-08-26 18:59:27 字数 525 浏览 12 评论 0 原文

如何使用 Servlet 访问 JSP 的 HTML 使用,而不必将所有面向客户端的页面都称为 *.jsp?
我宁愿这样做,也不愿使用所有的response.write()东西,因为我认为当它都是干净的“HTML”时,它更容易阅读和维护。
这是公平的评价吗?

编辑:我想要的是让 Servlet 将内容输出到屏幕,而不必重定向到 .jsp 文件。
通过这种方式,我可以编写所有 JSP 内容,但是当需要显示它时,用户看到的 URL 页面本质上是“http://blah.com/posts/post-id” 是 servlet 的地址,而不是“http://blah.com/posts.jsp?pos=post-id"。
但我仍然会在外部 .jsp 中编写所有表示逻辑。

How can I use Servlets to access the HTML uses of having JSP without having to have all my client-facing pages called *.jsp?
I would rather do this than using all the response.write() stuff because I think it is easier to read and maintain when it is all clean "HTML".
Is this is fair assesment?

EDIT: What I'm going for is having the Servlets output things to the screen without having to redirect to a .jsp file.
In this way, I could write all the JSP stuff, but when it comes time to display it, the page the URL the user sees is essentially, "http://blah.com/posts/post-id" which is the address of the servlet and not "http://blah.com/posts.jsp?pos=post-id".
But I would still write all presentation logic in an external .jsp.

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

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

发布评论

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

评论(5

逆光飞翔i 2024-09-02 18:59:27

只需将 JSP 隐藏在 /WEB-INF 文件夹中,这样就没有人可以直接访问它,并创建一个将请求转发到此 JSP 文件的 servlet。不要进行重定向,否则您将看到新的 URL 反映在地址栏中。例如,

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String postId = request.getPathInfo();
    // Do your business thing here. Any results can be placed in request scope. E.g.
    request.setAttribute("post", post); // post is a bean containing information you'd like to display in JSP.
    // Then forward request to JSP file.
    request.getRequestDispatcher("/WEB-INF/posts.jsp").forward(request, response);
}

将此 servlet 映射到 /posts/*url-pattern 上。

/WEB-INF/posts.jsp 中,使用标签库来控制页面流,并使用 EL 来访问数据。例如

<h2>${post.title}</h2>
<p><fmt:formatDate value="${post.date}" type="date" /> - ${post.message}</p>

,最后只需通过http://example.com/posts/postid调用servlet。 /postid 部分将通过 HttpServletRequest#getPathInfo()。您需要自己解析该值并用它来做业务。

Just hide the JSP away in /WEB-INF folder so that noone can access it directly and create a servlet which forwards the request to this JSP file. Don't do a redirect, else you will see the new URL being reflected in the address bar. E.g.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String postId = request.getPathInfo();
    // Do your business thing here. Any results can be placed in request scope. E.g.
    request.setAttribute("post", post); // post is a bean containing information you'd like to display in JSP.
    // Then forward request to JSP file.
    request.getRequestDispatcher("/WEB-INF/posts.jsp").forward(request, response);
}

Map this servlet on an url-pattern of /posts/*.

In the /WEB-INF/posts.jsp make use of taglibs to control page flow and EL to access the data. E.g.

<h2>${post.title}</h2>
<p><fmt:formatDate value="${post.date}" type="date" /> - ${post.message}</p>

Finally just invoke the servlet by http://example.com/posts/postid. The /postid part will be available by HttpServletRequest#getPathInfo(). You need to parse the value yourself and do the business thing with it.

软甜啾 2024-09-02 18:59:27

我不完全确定你在这里问什么。您可以让 servlet 本身编写 HTML,但这根本不干净。

另一种方法是让 servlet 通过模板引擎创建 HTML,例如 VelocityFreemarker。对于您的特定应用程序来说,模板中的语法可能更清晰,尽管功能不太齐全。

I'm not entirely sure what you're asking here. You can ge servlets themselves to write HTML, but that's not clean at all.

An alternative is to get your servlets to create HTML via a templating engine, such as Velocity or Freemarker. The syntax in the templates may be cleaner for your particular application, if less fully featured.

烦人精 2024-09-02 18:59:27

回到古代(想想 98 年...),这被称为“Model 2 架构”:servlet 接收请求,处理它,并将请求移交给处理视图的 JSP 页面。

有关示例,请参阅本文了解如何完成此操作,或者只需搜索“JSP Model 2”。

编辑:为此,您可以使用 RequestDispatcher.include() 而不是上一篇文章中所述的 forward()。其余的应该仍然适用。

Back in ancient times (think '98...) this was called a "Model 2 architecture": a servlet received the request, processed it, and handed the request over to a JSP page that handled the view.

See this article for one example of how this is done, or simply search for "JSP Model 2".

Edit: for that, you can use RequestDispatcher.include() instead of forward() as described in the previous article. The rest should still be applicable.

深陷 2024-09-02 18:59:27

如果我理解正确的话,您想对用户隐藏 *.jsp 扩展名,对吗?

在这种情况下,当您的 Servlet 重定向到 jsp 页面时,请执行以下操作:

RequestDispatcher disp = request.getRequestDispatcher("hidden.jsp");
disp.forward(request,response);   

通过使用 Request Dispatcher 而不是重定向,您可以将 .jsp 扩展名“隐藏”在 servlet 名称后面。但是,如果您的 JSP 页面重定向到另一个 JSP 页面,则这将不起作用。
如果您希望 .jsp 文件可见,请使用 response.encodeURLresponse.sendRedirect

If I understand correctly you want to hide *.jsp extension from user, right?

In that case when your Servlet redirects to a jsp page have it do this:

RequestDispatcher disp = request.getRequestDispatcher("hidden.jsp");
disp.forward(request,response);   

By using Request Dispatcher instead of redirect you "hide" your .jsp extension behind the servlet name. However in case your JSP page redirects to another JSP page this won't work.
If you want the .jsp file to be visible use response.encodeURL or response.sendRedirect

南…巷孤猫 2024-09-02 18:59:27

我认为您正在寻找 前端控制器模式 - 这是“JSP Model 2”Web 应用程序(如 @andri 提到的)和几乎所有(数百个?)Java Web 框架的基础。

I think you're looking for the Front Controller Pattern - this is the basis of "JSP Model 2" web apps (as @andri mentioned) and pretty much all the (hundreds?) of Java web frameworks.

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