使用 doPost 但仍停留在同一页面

发布于 2024-10-18 08:19:23 字数 913 浏览 1 评论 0原文

您好,我正在开发一个带有 JSP 和 servlet 的基于 Web 的应用程序。我有一个页面,其中有一个按钮。我编写了一些 JavaScript 来在单击按钮时弹出一个小窗口,并且它还会调用 doPost 方法。代码是这样的——

]
"<form name='downloadFrm1' method='post' action='S2VServlet' 
..  ..
"<input name='submitBtn' type='submit'  value='Download Files With Warnings' onClick="+popup+">\n"+

"</form><BR></div><p><BR></p> \n";

但是,我调用 doPost 方法只是为了做一些内部工作。我不希望父页面重定向到任何地方。但发生的情况是,当我单击按钮时,会出现小弹出窗口,但父页面也会移动到空白页面。如何使父页面保持原样而不被重定向? doPost 的代码是这样的——

    public void doPost(HttpServletRequest p_req, HttpServletResponse p_res) throws IOException, ServletException {
        try {
            String downloadKey = p_req.getParameter(DOWNLOAD_KEY_ELEMENT);
            ....
            if (//put my condition)) {
//just set some internal value and return
                ....
                return;
            }

Hi I am working on a webbased application with JSPs and servlets. I have a page which has a button in it. I have written some javascript to pop-up a small window when the button is clicked and also it invokes the doPost method. The code is something like this --

]
"<form name='downloadFrm1' method='post' action='S2VServlet' 
..  ..
"<input name='submitBtn' type='submit'  value='Download Files With Warnings' onClick="+popup+">\n"+

"</form><BR></div><p><BR></p> \n";

However, I invoke the doPost method only to do some internal work. I don't want the parent page to be redirected anywhere. But what is happening is that when I click on the button the small popup window appears but the parent page also moves to a blank page. How can I make the parent page stay as is and not get redirected ? The code for doPost is something like ths--

    public void doPost(HttpServletRequest p_req, HttpServletResponse p_res) throws IOException, ServletException {
        try {
            String downloadKey = p_req.getParameter(DOWNLOAD_KEY_ELEMENT);
            ....
            if (//put my condition)) {
//just set some internal value and return
                ....
                return;
            }

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

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

发布评论

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

评论(2

等风也等你 2024-10-25 08:19:23

如果没有ajax (javascript),你就无法做到这一点。标准请求/响应周期需要更改页面。

看一下 jQuery。您可以执行 $.post("S2VServlet", {param:param}) ,它将在后台执行。

You can't do it without ajax (javascript). The standard request/response cycle requires a change in the page.

Take a look at jQuery. You can do $.post("S2VServlet", {param:param}) which will execute in the background.

失去的东西太少 2024-10-25 08:19:23

我认为你可以通过 doGet 来结束你的 doPost 方法。这样做会“重复”该页面,但用户认为他/她没有移动。

I think you can end your doPost method doing doGet. Doing so will "repeat" the page, but the user thinks he/she has not moved.

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