java 如何在jsp-servlets中与请求页面相同的页面上显示结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10198893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:01:58  来源:igfitidea点击:

How to display results on same page as requesting page in jsp-servlets

javajspservlets

提问by Dhruv

I developing this web app using jsp and servlets. I was wondering how to display results on same page as on from which the request was made. For example, usually what I do is that a request is sent from one jsp to a servlet and then servlet sends the response/results on separate jsp. I am want to show a database results on same page. How can this be done?..A code snippet would be appreciated. Thanks

我使用 jsp 和 servlet 开发这个 web 应用程序。我想知道如何在与发出请求的页面相同的页面上显示结果。例如,通常我所做的是将请求从一个 jsp 发送到 servlet,然后 servlet 在单独的 jsp 上发送响应/结果。我想在同一页面上显示数据库结果。如何做到这一点?...代码片段将不胜感激。谢谢

回答by BalusC

Just set the necessary data in the request scope and use RequestDispatcher#forward()to forward the control to the desired JSP which in turn can generate the appropriate HTML based on the results.

只需在请求范围内设置必要的数据,并用于RequestDispatcher#forward()将控件转发到所需的 JSP,后者又可以根据结果生成适当的 HTML。

request.setAttribute("results", results);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

This is also demonstrated in the hello world examples in our servlets wiki page.

这也在我们的 servlet wiki 页面中的 hello world 示例中得到演示。

回答by Carlos Gavidia-Calderon

That's totally posible, but you need to include JavaScript in your toolset. What I'm suggesting is an AJAX approach in your application, and that implies intensive use of client-side scripting (i.e JavaScript).

这是完全可能的,但您需要在您的工具集中包含 JavaScript。我建议在您的应用程序中使用 AJAX 方法,这意味着大量使用客户端脚本(即 JavaScript)。

This tutorialfrom DeveloperWorks makes a pure JavaScript/Servlet approach. If you want to do more advanced stuff, I strongly suggest you to use a JavaScript Framework with AJAX support, like JQuery.

来自DeveloperWorks 的本教程采用纯JavaScript/Servlet 方法。如果你想做更高级的事情,我强烈建议你使用支持 AJAX 的 JavaScript 框架,比如JQuery

回答by Luiggi Mendoza

From your servlet, send a redirect to your same page. Also, set some attributes in request and show it in your page, e.g. save List<MyObject>in request, then in your jsp read the list from request and show the values in a table.

从您的 servlet,将重定向发送到您的同一页面。另外,在请求中设置一些属性并将其显示在您的页面中,例如保存List<MyObject>在请求中,然后在您的jsp 中从请求中读取列表并在表中显示值。