java 从一个 JSP 文件调用另一个 JSP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5288726/
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
Call from one JSP file to another JSP
提问by Delashmate
I need to call from one JSP to another, do some stuff over there..
我需要从一个 JSP 调用到另一个,在那里做一些事情..
I am inside the caller jsp, inside his handleReqeust(HttpServletReqeust request) method I am trying to forward the request to another JSP, to call the other JSP file to his handleRequest(HttpServletReqeust request) off course with the request object
我在调用者jsp里面,在他的handleReqeust(HttpServletReqeust request)方法里面我试图将请求转发到另一个JSP,用请求对象将另一个JSP文件调用到他的handleRequest(HttpServletReqeust request)
I tried it:
我尝试过这个:
RequestDispatcher dispatcher = request.getRequestDispatcher("/theSecondJspFile.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
but to make it work I need for it the object response, but I don't have it, I am sure I missed something basic, but what?
但是为了使它工作,我需要对象响应,但我没有它,我确定我错过了一些基本的东西,但是什么?
From my question you can see, I don't have solid backround in java, so please correct me, or refer me to good guide if you feel it necessary
从我的问题中你可以看到,我在 Java 方面没有扎实的背景,所以请纠正我,或者如果你觉得有必要,请给我推荐好的指南
Thanks.
谢谢。
-------------------Edit--------------------------------------
- - - - - - - - - -编辑 - - - - - - - - - - - - - - - --------
I don't need a redirect, I just want to call another JSP file to his handleRequest method I think it relate to HTML
我不需要重定向,我只想调用另一个 JSP 文件到他的 handleRequest 方法我认为它与 HTML 有关
采纳答案by Delashmate
I just added empty Iframe, and set his URL when I needed to call him
我只是添加了空的 iframe,并在我需要给他打电话时设置了他的 URL
回答by Satadru Biswas
What I can make out of your question is that you need to redirect to a secondpage.html from firstpage.html with some data. You can use both GET or POST method to send the data.
我可以从您的问题中得出的是,您需要使用一些数据从 firstpage.html 重定向到 secondpage.html。您可以使用 GET 或 POST 方法发送数据。
For the GET method just redirect to secondpage,html?data=value. The data will be available in the HttpRequest parameter in the controller of the secondpage.html where it can be used as required.
对于 GET 方法,只需重定向到 secondpage,html?data=value。数据将在 secondpage.html 的控制器中的 HttpRequest 参数中可用,可以根据需要使用。
For the POST method you would need to post the data (using a form on firstpage.html) to secondpage.html. In the controller of the secondpage.html the data should be available in a similar way as before.
对于 POST 方法,您需要将数据(使用 firstpage.html 上的表单)发布到 secondpage.html。在 secondpage.html 的控制器中,数据应该以与以前类似的方式可用。
回答by Nicolas Bousquet
回答by Nishan
I can not be sure, but what I think you are trying to do is to include a jsp page on to your jsp page and use the the objects and other variables declared in the first jsp page in your second.
我不能确定,但我认为您想要做的是在您的 jsp 页面上包含一个 jsp 页面,并在您的第二个页面中使用第一个 jsp 页面中声明的对象和其他变量。
<%@ include file="mypage.jsp" %>
should help you do this.
<%@ include file="mypage.jsp" %>
应该可以帮助你做到这一点。
If this is not what you are looking for, please make your question tad bit more clear. some code will help really.
如果这不是您要找的,请让您的问题更清楚一点。一些代码真的会有所帮助。
回答by BalusC
JSPs are supposed to be used to present the final result. JSPs are not supposed to be part of business tasks leading to this result. There you use normal Java classes for, starting with a servlet class. Let the HTTP request (the one which you enter in browser address bar or specify in some HTML link or form) point to the URL of the servlet instead. This way you can write Java code in the servlet the usual way to invoke other Java classes/methods and finally forward the request to a certain JSP file based on the outcome of the result and then just let that JSP present the final result.
应该使用 JSP 来呈现最终结果。JSP 不应成为导致此结果的业务任务的一部分。在那里您可以使用普通的 Java 类,从 servlet 类开始。让 HTTP 请求(您在浏览器地址栏中输入或在某些 HTML 链接或表单中指定的请求)指向 servlet 的 URL。通过这种方式,您可以在 servlet 中编写 Java 代码,以调用其他 Java 类/方法的常用方式,最后根据结果的结果将请求转发到某个 JSP 文件,然后让该 JSP 呈现最终结果。
To start with servlets, I'd suggest to read our Servlets info page.
要从 servlet 开始,我建议阅读我们的 Servlet 信息页面。